1 The syntax of both printf and scanf

  • toy example
#include <stdio.h>
int main()
{
    //Input: 1:2:3
    //Output:
    //h = 1
    //m = 2
    //s = 3
    int h,m,s;
    scanf("%d:%d:%d",&h,&m,&s);
    printf("h = %d\nm = %d\ns = %d\n",h,m,s);
    return 0;
}
  • different radices: binary, decimal, octal, and hexadecimal systems
#include <stdio.h>
int main()
{
    //Input: 123 123 123
    //Output: 123 83 291
    int a,b,c;
    scanf("%d,%o,%x",&a,&b,&c);
    printf("a = %d,\nb = %d,\nc = %d\n",a,b,c);
    printf("\n\n");
    printf("a = %d,\nb = %o,\nc = %x\n",a,b,c);
    return 0;
}
#include<stdio.h>
int main(){
    int a=123;
    printf("%o,\t%#o,\t%X,\t%#X\n",a,a,a,a);
    printf("\n\n");
    printf("%#X,\t%08X,\t%p\n",&a,&a,&a);
    return 0;
}
  • usage of m.n
#include <stdio.h>
int main(){
    int b=1234;
    float f=123.456;
    char ch='a';
    printf("%8d,\n%2d\n",b,b);
    printf("%f,\n%8f,\n%8.1f,\n%.2f,\n%.2e\n",f,f,f,f,f);
    printf("%3c\n",ch);
    printf("\n\n");

    static char a[]="Hello,world!";
    printf("%s\n%15s\n%10.5s\n%2.5s\n%.3s\n",a,a,a,a,a);
    return 0;
}
  • usage of -m.n
#include<stdio.h>
int main(){
    int a=1234;
    float f=123.456;
    static char c[]="Hello,world!";
    printf("%8d,\n%-8d\n",a,a);
    printf("%10.2f,\n%-10.1f\n",f,f);
    printf("%10.5s,\n%-10.3s\n",c,c);
    return 0;
}

2 Sugar functions

  • Find two roots of a quadratic equation with coefficients a, b, and c.
#include <stdio.h>
int main()
{
    //Find two roots of a quadratic equation with coefficients a, b, and c.
    float a,b,c,delta,x1,x2,p,q;
    scanf("%f,%f,%f",&a,&b,&c);
    delta = b*b-4*a*c;
    p = -b/(2*a);
    q = sqrt(delta)/(2*a);
    x1 = p+q;
    x2 = p-q;
    printf("\nx1 = %5.2f\nx2 = %5.2f\n",x1,x2);
    printf("\nx1 = %f\nx2 = %f\n",x1,x2);
    return 0;
}
  • calculate trianges area
#include <math.h>
#include <stdio.h>
int main(){
    // Input three edges a, b, and c of a triangle
    // Output the area of this triangle
    float a,b,c,s,area;
    scanf("%f,%f,%f",&a,&b,&c);
    s=(a+b+c)/2;
    area=sqrt(s*(s-a)*(s-b)*(s-c));
    printf("a=%7.2f,\t b=%7.2f,\t c=%7.2f,\t s=%7.2f\n",a,b,c,s);
    printf("area=%7.2f\n",area);
    return 0;
}
  • Find three-digit positive integers x=a*100+b*10+c subject to x=a^3+b^3+c^3
#include <stdio.h>
int main(){
    // Find three-digit positive integers x=a*100+b*10+c
    // subject to x=a^3+b^3+c^3
    
    int a,b,c,x;
    for(x=100;x<999;x++){
        a = x/100;
        b = (x-a*100)/10;
        c = x-a*100-b*10;
        if(a*a*a+b*b*b+c*c*c==x)
            printf("%d\n",x);
    }
    return 0;
}
  • Find three integers a, b, and c subject to two three-digit positive integers x1=a*100+b*10+c and x2 = c*100+b*10+a and x1+x2=y, where y is a four-digit positive integers
#include <stdio.h>
int main(){
    // 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,y;
    printf("Please input a four-digit integer y = ");
    scanf("%d",&y);
    for(x=100;x<999;x++){
        a = x/100;
        b = (x-a*100)/10;
        c = x%10;
        if(a*100+b*10+c+c*100+b*10+a==y)
            printf("a = %d, b = %d, c = %d\n",a,b,c);
    }
    return 0;
}
  • common error. What happens in the follows?
#include <stdio.h>
int main(){
    // 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,y;
    printf("Please input a four-digit integer y = ");
    scanf("%d",&y);
    for(x=100;x<999;x++);{
        a = x/100;
        b = (x-a*100)/10;
        c = x%10;
        if(a*100+b*10+c+c*100+b*10+a==y)
            printf("a = %d, b = %d, c = %d\n",a,b,c);
    }
    return 0;
}
  • determine whether an integer x is a prime or not
#include <stdio.h>
int main(){
    // Is x a prime?

    int i,x, max;
    printf("Please input a positive integer x \n");
    scanf("%d",&x);

    max = sqrt(1.0*x)+1;
    for(i=2; i<max; i++){
        if(x%i==0) break;
    }
    if(i>=max){
        printf("Input integer %d is a prime. \n",x);
    }
    else{
        printf("Input integer %d is not a prime. \n",x);
    }
    return 0;
}

3 Problem in the previous week

  • Find two nonnegative integers a and b such that the four-digit positive integer a2b3 can be divided by 23, where a2b3 = a\*1000+2\*100+b\*10+3.
#include <stdio.h>
int main(){
    // Find tow integers a and b such that
    // the four-digit integer a2b3 can be divided by 23,
    // where a2b3 = a*1000+2*100+b*10+3
    int i, j, c;
    for (i = 9; i>0; i--);{
        for (j = 0; j<10; j++){
            c = i * 1000 + j * 10 + 203;
            if (!(c % 23))
                printf("%d\n", c);
        }
    }
    return 0;
}

4 Problems

  • Find all primes between 11 to 1000.
  • 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.