Loop (while)

Exercise 1

Below is a source code to show when sum of saving exceeds JPY10,000 if one keeps saving JPY1 today, JPY2 tomorrow and JPY4 the day after tomorrow. Write the source code and save it as "while1.c". After compilation, confirm by executing it with different input data.

#include <stdio.h>

int main(void){

    int i, j, sum;

    i = 1;
    j = 1;
    sum = 0;

    while (sum < 10000){
       sum += j;
       printf("Sum of saving up to day %2d =  %8d JPY\n", i, sum);
       i++;
       j *= 2;
    }

    return (0);
}
$ ./a.out
Sum of saving up to day  1 =         1 JPY
Sum of saving up to day  2 =         3 JPY
Sum of saving up to day  3 =         7 JPY
Sum of saving up to day  4 =        15 JPY
Sum of saving up to day  5 =        31 JPY
Sum of saving up to day  6 =        63 JPY
Sum of saving up to day  7 =       127 JPY
Sum of saving up to day  8 =       255 JPY
Sum of saving up to day  9 =       511 JPY
Sum of saving up to day 10 =      1023 JPY
Sum of saving up to day 11 =      2047 JPY
Sum of saving up to day 12 =      4095 JPY
Sum of saving up to day 13 =      8191 JPY
Sum of saving up to day 14 =     16383 JPY

Exercise 2

Below is a source code to show sum and mean of input data including decimal. Write the source code and save it as "while2.c". After compilation, confirm by executing it with different input data.

#include <stdio.h>

int main(void){

    int n;
    double dat, sum, mean;

    sum=0.0;
    n=0;
    while (scanf("%lf", &dat)!=EOF){
        sum += dat;
        n++;
    }
    printf("Sum  = %9.4f\n", sum);

    mean = sum/n;
    printf("Mean = %9.4f\n", mean);

    return (0);
}
(Execution result)
$ ./a.out
1
2.1
3.2
0.8
5
^d  <- Ctrl-d
Sum  =   12.1000
Mean =    2.4200

Exercise 3

Write a below source code and save it as "while3.c". After compilation, confirm the difference between results of Exercise 2 and results of Exercise 3.

#include <stdio.h>

int main(void){

    int n, sum, mean;
    double dat;

    sum=0;
    n=0;
    while (scanf("%lf", &dat)!=EOF){
        sum += dat;
        n++;
    }
    printf("Sum  = %4d\n", sum);

    mean = sum/n;
    printf("Mean = %4d\n", mean);

    return (0);
}
(Execution result)
$ ./a.out
1
2.1
3.2
0.8
5
^d 
Sum  =   ******* <- concealed by *****
Mean =   *******

Exercise 4

For a group whose student number is known, we want to calculate means of english, mathematics and physics, respectively. Using while-loop, program to meet this requirement and save such a source code as "while4.c".

(Execution result)
$ ./a.out
Input number of students
3

English score of student 1 50
Math score of student 1 70
Physics score of student 1 60

English score of student 2 40
Math score of student 2 20
Physics score of student 2 30

English score of student 3 100
Math score of student 3 100
Physics score of student 3 90

Mean of english = 63.333
Mean of math    = 63.333
Mean of physics = 60.000

Exercise 5

We want to ask age and show different message based on the age. Using if-statement, program and save such a source code as "while5.c".

(Execution result)
$ ./a.out
How old are you? 30
You are 30 years old.
$
$ ./a.out
How old are you? 105
You are 105 years old. You live long.
$
$ ./a.out
How old are you? -10
Input proper age.
$

Exercise 6

In addition to while5.c, if input age is not appropriate, we want to repeat to show input message as shown below. Using while-loop, program and save such a source code as "while6.c".

(Execution result)
$ ./a.out
How old are you? 30
You are 30 years old.
$
$ ./a.out
How old are you? 105
You are 105 years old. You live long.
$
$ ./a.out
How old are you? -10
Input proper age.
How old are you? -10
Input proper age.
How old are you? -3
Input proper age.
How old are you? 15
You are 15 years old.
$

Lessons for C programming
Junichi Susaki, Kyoto University