1 The syntax of both if and if-else

  • toy example
#include <stdio.h>
int main()
{
    int a=1,x;
    x = 0;
    if(!a<0) x = 10;
    printf("x=%d\n",x);

    x = 0;
    if(!(a<0)) x = 10;
    printf("x=%d\n",x);
    return 0;
}
  • Relational expressions and logical operators
#include<stdio.h>
int main()
{
    int a,b,c,d=1,x=1;
    a = b = c = 0;
    if((d = a<b++)&&c++)
        x = 1;
    printf("a = %d,\t b = %d,\t c = %d,\t d = %d\n",a,b,c,d);

    a = 0;
    b = c = 1;
    if(d = a<b++&&c++)
        x = 1;
    printf("a = %d,\t b = %d,\t c = %d,\t d = %d\n",a,b,c,d);

    a = c = 0;
    b = 1;
    if(d = a<b++&&c++)
        x = 1;
    printf("a = %d,\t b = %d,\t c = %d,\t d = %d\n",a,b,c,d);
    return 0;
}
#include<stdio.h>
int main()
{
    int a,b,c,d=1;
    a = b = c = 0;
    if(a&&b++&&(c++))
        d = 10;
    printf("a = %d,\t b = %d,\t c = %d,\t d = %d\n",a,b,c,d);

    a = 1;
    b = c = 0;
    if(a&&(b++)&&(c++))
        d = 10;
    printf("a = %d,\t b = %d,\t c = %d,\t d = %d\n",a,b,c,d);

    a = 1;
    b = c = 0;
    if(a&&++b&&(c++))
        d = 10;
    printf("a = %d,\t b = %d,\t c = %d,\t d = %d\n",a,b,c,d);

    a = b = c = 0;
    if(a||(b++)||(c++))
        d = 10;
    printf("a = %d,\t b = %d,\t c = %d,\t d = %d\n",a,b,c,d);

    a = 1;
    b = c = 0;
    if(a||(b++)||(c++))
        d = 10;
    printf("a = %d,\t b = %d,\t c = %d,\t d = %d\n",a,b,c,d);
    return 0;
}
  • Nested if statements and the if-else chain
#include<stdio.h>
int main()
{
    // input 'a' or 'A', print a plus b
    // input 'b' or 'B', print a times b
    // input 'c' or 'C', print a minus b
    // output a voice warning otherwise
    char ch;
    int a=15,b=23;
    ch = getchar();
    printf("a = %d,\tb = %d\n",a,b);
    if(ch=='A'||ch=='a')
        printf("a+b = %d\n",a+b);
    else if(ch=='B'||ch=='b')
        printf("a*b = %d\n",a*b);
    else if(ch=='C'||ch=='c')
        printf("a-b = %d\n",a-b);
    else
        putchar('\a');

    return 0;
}

2 Sugar functions

  • 100 dollars buy 100 chickens. One buys 100 chickens using 100 dollars. A hen costs 3 dollars, a rooster costs 2 dollars, and 3 chicks cost one dollar. How many hens, roosters and chick there are amount 100 chickens?
#include <stdio.h>
int main(){
    // 100 dollars buy 100 chickens
    // 1 hen for 3 dollars  
    // 1 rooster for 2 dollars
    // 3 chick for 1 dollar
    int a,b,c;
    // a --- chick;
    // b --- rooster;
    // c --- hen;
    a = 3;
    while(a<98){
        for(b=1; b<98; b++){
            for(c=1; c<98; c++){
                if(a+b+c==100 && a/3+2*b+3*c==100)
                    printf("chicks: %d,\t roosters: %d,\t hens: %d\n",a,b,c);
            }
        }
        a = a+3;
    }
    return 0;
}
  • The sum of the squares of the first ten natural numbers is, \[1^2 + 2^2 + ... + 10^2 = 385\] The square of the sum of the first ten natural numbers is, \[(1 + 2 + ... + 10)^2 = 55^2 = 3025\] Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025-385=2640. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

    • The formula of the difference is \[diff=\frac{n(n+1)(n-1)(3n+2)}{12}.\]
#include<stdio.h>
int main(){
    // sum1 = 1^2+2^2+...+n^2
    // sum2 = (1+2+...+n)^2
    // Find the difference sum2-sum1 
    int diff(int n);
    int count;
    count = diff(50);
    printf("count = %d\n",count);

    return 0;
}

int diff(int n){
    int i,sum=1,sum1=1;
    for(i=2;i<=n;i++){
        sum+=i*i;
        sum1+=i;
    }
    return sum1*sum1-sum;
}
  • Find all perfect numbers not greater than N
#include <stdio.h>
int main(){
    // Find all perfect numbers not greater than N
    int isPerfectNumber(int perfectNum);
    int i,N;
    printf("The number N is ");
    scanf("%d",&N);
    for(i=6;i<=N;i++){
        if(isPerfectNumber(i))
            printf("%d is perfect number.\n", i);
    }
    return 0;
}

int isPerfectNumber(int perfectNum){
    // check if the inputted integer is a perfect number
    int j,sum=1;
    for(j=2; j<=perfectNum/2; j++){
        if(perfectNum%j==0){
            sum += j;
        }
    }
    /*
    int flag = 0;
    if(sum==perfectNum) flag = 1;
    return flag;
    */
    if(sum==perfectNum){
        return 1;
    }
    else
        return 0;
}

3 Problems in the previous week

  • Find all primes between 11 to 1000.
#include <stdio.h>
#include <math.h>
int main(){
    // Find all primes between 11 to x
    int i,j,x;

    printf("Please input an integer x not smaller than 11 \n");
    scanf("%d",&x);

    for(i=11; i<=x; i++){
        for(j=2; j<sqrt(1.0*i); j++){
            if(i%j==0) break;
        }
        if(j>=i){
            printf("%d \t",i);
        }
    }
    return 0;
}
  • Count how many four-digit integers y that is the sum of two three-digit integers x1 and x2, where x1=a*100+b*10+c and x1=c*100+b*10+a.
#include <stdio.h>
int main(){
    // Count how many four-digit integers y that
    // is the sum of two three-digit integers x1 and x2, 
    // where x1 = a*100 + b*10 + c and x1 = c*100 + b*10 + a.    
    int y,count=0;
    int check_y_satisfy_condition(int y);
    for(y=1000;y<2000;y++){
        if(check_y_satisfy_condition(y)){
            count++;
            printf("%d\t",y);
            if(!(count%7))
                printf("\n");
        }
    }
    printf("\n\nthe total number is %d",count);
    return 0;
}

int check_y_satisfy_condition(int y)
{
    // two three-digit integers x1=a*100+b*10+c and
    // x2 = c*100+b*10+a subject to x1+x2=y
    // Example: Suppose y = 1333. We have
    //   a b c
    // + c b a
    // --------
    // 1 3 3 3
    int a,b,c,x,flag=0;
    for(x=100;x<1000;x++){
        a = x/100;
        b = (x-a*100)/10;
        c = x%10;
        if(a*100+b*10+c+c*100+b*10+a==y){
            flag = 1;
            break;
        }
    }
    return flag;
}

4 Problem

  • Calculate 1/2019 with 100-digit precesion. Print it with 10 digits per line.