Loop (for)

Exercise 1

Below is a source code to show string as many times as input figure. Write the source code and save it as "for1.c". After compilation, confirm by executing it with different input data.

#include <stdio.h>

int main(void) {

    int i, x;

    printf("Input integer ");
    scanf("%d", &x);

    for (i=1; i<=x; i++){ 
        printf("%d times\n", i);
    }

    return (0);
}
(Execution result)
$ ./a.out
Input integer 5
1 times
2 times
3 times
4 times
5 times

Exercise 2

A source code is shown to compare sum of 1 to input figure, but part of the code is hidden by "******". Write the source code, complement the expression hidden by "******" and save it as "if2.c". After compilation, confirm by executing it with different input data.

#include <stdio.h>

int main(void) {

    int i, x, sum=0;

    printf("Input integer ");
    scanf("%d", &x);

    for (i=1; i<=x; i++){
        ******************* 
    }
    printf("Sum of 1 to %d = %d\n", x, sum);

    return (0);
}
(Execution result)
$ ./a.out
Input integer 6
Sum of 1 to 6 = 21

Exercise 3

Below is a source code to show a figure by "*". Write the source code and save it as "for3.c". After compilation, confirm by executing it.

#include <stdio.h>

int main(void) {

    int i, j;

    for (i=0; i<10; i++){
        for (j=0; j<10-i; j++){
            printf("*");
        }
        printf("\n");
    }

    return (0);
}
(Execution result)
$ ./a.out
**********
*********
********
*******
******
*****
****
***
**
*

Exercise 4

Below is a source code to show a figure by "*". Write the source code and save it as "for4.c". After compilation, confirm by executing it.

#include <stdio.h>

int main(void) {

    int i, j;

    for (i=0; i<10; i++){
        for (j=0; j<i; j++){
            printf(" ");
        }
        for (j=0; j<10; j++){
            printf("*");
        }
        printf("\n");
    }

    return (0);
}
(Execution result)
$ ./a.out
**********
 **********
  **********
   **********
    **********
     **********
      **********
       **********
        **********
         **********

Exercise 5

Considering source codes of Exercises 4 and 5, program to show a below figure and save the source code as "for5.c".

(Execution result)
$ ./a.out
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *

Exercise 6

Considering the source code of Exercise 6, program to show a below figure and save the source code as "for6.c".

(Execution result)
$ ./a.out
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *

Exercise 7

Using for-loop, program to show a below multiplication table and save the source code as "for7.c". Be careful that each figure should be right-aligned.

(Execution result)
$ ./a.out
   1   2   3   4   5   6   7   8   9
   2   4   6   8  10  12  14  16  18
   3   6   9  12  15  18  21  24  27
   4   8  12  16  20  24  28  32  36
   5  10  15  20  25  30  35  40  45
   6  12  18  24  30  36  42  48  54
   7  14  21  28  35  42  49  56  63
   8  16  24  32  40  48  56  64  72
   9  18  27  36  45  54  63  72  81

Exercise 8

We want to show calendar of a specific month. Using for-loop, program to show a below calendar and save the source code as "for8.c". Input data are (1) day of the week of an input month, and (2) number of days of the input month. Be careful that each figure should be right-aligned.

Caution: When input figure is not appropriate, e.g. day of the week is not within 0 to 6, or month is not within 1 to 12, an error message should be shown.

(Execution result)
$ ./a.out
Calendar of a specific month will be shown.
Which day of the week does the month start?
Input figure among following figures.
0:Sun, 1:Mon, 2:Tue, 3:Wed, 4:Thu, 5:Fri, 6:Sat
5
How many days does the month have? Input figure
28
 Su Mo Tu We Th Fr Sa
                 1  2
  3  4  5  6  7  8  9
 10 11 12 13 14 15 16
 17 18 19 20 21 22 23
 24 25 26 27 28
$
$ ./a.out
Calendar of a specific month will be shown.
Which day of the week does the month start?
Input figure among following figures.
0:Sun, 1:Mon, 2:Tue, 3:Wed, 4:Thu, 5:Fri, 6:Sat
0
How many days does the month have? Input figure
28
 Su Mo Tu We Th Fr Sa
  1  2  3  4  5  6  7
  8  9 10 11 12 13 14
 15 16 17 18 19 20 21
 22 23 24 25 26 27 28
$
$ ./a.out
Calendar of a specific month will be shown.
Which day of the week does the month start?
Input figure among following figures.
0:Sun, 1:Mon, 2:Tue, 3:Wed, 4:Thu, 5:Fri, 6:Sat
0
How many days does the month have? Input figure
31
 Su Mo Tu We Th Fr Sa
  1  2  3  4  5  6  7
  8  9 10 11 12 13 14
 15 16 17 18 19 20 21
 22 23 24 25 26 27 28
 29 30 31
$

Exercise 9

In addition to Exercise 8, we want to show calendar of specific three continuous months. Using for-loop, program to show below calendars and save the source code as "for9.c". Input data are (1) day of the week of the first month, and (2) number of days of each month (first, second and third). Be careful that each figure should be right-aligned.

Caution: When input figure is not appropriate, e.g. day of the week is not within 0 to 6, or month is not within 1 to 12, an error message should be shown.

Caution: Use of function may make your coding more efficient.

(Execution result)
$ ./a.out
Calendar of three continuous months will be shown.
Which month is the first month?
11
Which day of the week does the month start with?
Input figure among following figures.
0:Sun, 1:Mon, 2:Tue, 3:Wed, 4:Thu, 5:Fri, 6:Sat
3
How many days do the first, second and third month have? Input figure
30 31 31
         11
 Su Mo Tu We Th Fr Sa
           1  2  3  4
  5  6  7  8  9 10 11
 12 13 14 15 16 17 18
 19 20 21 22 23 24 25
 26 27 28 29 30

         12
 Su Mo Tu We Th Fr Sa
                 1  2
  3  4  5  6  7  8  9
 10 11 12 13 14 15 16
 17 18 19 20 21 22 23
 24 25 26 27 28 29 30
 31

          1
 Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6
  7  8  9 10 11 12 13
 14 15 16 17 18 19 20
 21 22 23 24 25 26 27
 28 29 30 31

$
$ ./a.out
Calendar of three continuous months will be shown.
Which month is the first month?
13
Input data error!
$
$ ./a.out
Calendar of three continuous months will be shown.
Which month is the first month?
3
Which day of the week does the month start with?
Input figure among following figures.
0:Sun, 1:Mon, 2:Tue, 3:Wed, 4:Thu, 5:Fri, 6:Sat
8
Input data error!
$

Exercise 10

In addition to Exercise 9, we want to show calendar of specific three continuous months. Using for-loop, program to show below calendars and save the source code as "for10.c".

Caution: When input figure is not appropriate, e.g. day of the week is not within 0 to 6, or month is not within 1 to 12, an error message should be shown.

Caution: Use of function may make your coding more efficient.

(Execution result)
$ ./a.out
Annual calendar will be shown.
Which day of the week is Jan 1?
Input figure among following figures.
0:Sun, 1:Mon, 2:Tue, 3:Wed, 4:Thu, 5:Fri, 6:Sat
6
If the year is leap-year, input 1. Otherwise, input 0.
0

          1                      2                      3          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                    1          1  2  3  4  5          1  2  3  4  5
  2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
  9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
 16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
 23 24 25 26 27 28 29   27 28                  27 28 29 30 31      
 30 31                                                             

          4                      5                      6          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                 1  2    1  2  3  4  5  6  7             1  2  3  4
  3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
 10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
 17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
 24 25 26 27 28 29 30   29 30 31               26 27 28 29 30      

          7                      8                      9          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                 1  2       1  2  3  4  5  6                1  2  3
  3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
 10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
 17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
 24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30   
 31                                                                

         10                     11                     12          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                    1          1  2  3  4  5                1  2  3
  2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
  9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
 16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
 23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
 30 31                                                             

$
$ ./a.out
Annual calendar will be shown.
Which day of the week is Jan 1?
Input figure among following figures.
0:Sun, 1:Mon, 2:Tue, 3:Wed, 4:Thu, 5:Fri, 6:Sat
1
If the year is leap-year, input 1. Otherwise, input 0.
1

          1                      2                      3          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6                1  2  3                   1  2
  7  8  9 10 11 12 13    4  5  6  7  8  9 10    3  4  5  6  7  8  9
 14 15 16 17 18 19 20   11 12 13 14 15 16 17   10 11 12 13 14 15 16
 21 22 23 24 25 26 27   18 19 20 21 22 23 24   17 18 19 20 21 22 23
 28 29 30 31            25 26 27 28 29         24 25 26 27 28 29 30
                                               31                  

          4                      5                      6          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6             1  2  3  4                      1
  7  8  9 10 11 12 13    5  6  7  8  9 10 11    2  3  4  5  6  7  8
 14 15 16 17 18 19 20   12 13 14 15 16 17 18    9 10 11 12 13 14 15
 21 22 23 24 25 26 27   19 20 21 22 23 24 25   16 17 18 19 20 21 22
 28 29 30               26 27 28 29 30 31      23 24 25 26 27 28 29
                                               30                  

          7                      8                      9          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6                1  2  3    1  2  3  4  5  6  7
  7  8  9 10 11 12 13    4  5  6  7  8  9 10    8  9 10 11 12 13 14
 14 15 16 17 18 19 20   11 12 13 14 15 16 17   15 16 17 18 19 20 21
 21 22 23 24 25 26 27   18 19 20 21 22 23 24   22 23 24 25 26 27 28
 28 29 30 31            25 26 27 28 29 30 31   29 30               

         10                     11                     12          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
        1  2  3  4  5                   1  2    1  2  3  4  5  6  7
  6  7  8  9 10 11 12    3  4  5  6  7  8  9    8  9 10 11 12 13 14
 13 14 15 16 17 18 19   10 11 12 13 14 15 16   15 16 17 18 19 20 21
 20 21 22 23 24 25 26   17 18 19 20 21 22 23   22 23 24 25 26 27 28
 27 28 29 30 31         24 25 26 27 28 29 30   29 30 31            

$

Exercise 11

Based on Exercise 10, we want to change number of months shown in a column, e.g. 1, 2 or 3. If other figure except 1 to 3 is input, calendar will be shown by 3 month in a column. Using for-loop, program to show below calendars and save the source code as "for11.c". Be careful that each figure should be right-aligned.

(Execution result)
$ ./a.out
Annual calendar will be shown.
Which day of the week is Jan 1?
Input figure among following figures.
0:Sun, 1:Mon, 2:Tue, 3:Wed, 4:Thu, 5:Fri, 6:Sat
1
If the year is leap-year, input 1. Otherwise, input 0.
1
Input number of months shown in a row.
1

          1          
 Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6
  7  8  9 10 11 12 13
 14 15 16 17 18 19 20
 21 22 23 24 25 26 27
 28 29 30 31         

          2          
 Su Mo Tu We Th Fr Sa
              1  2  3
  4  5  6  7  8  9 10
 11 12 13 14 15 16 17
 18 19 20 21 22 23 24
 25 26 27 28 29      

          3          
 Su Mo Tu We Th Fr Sa
                 1  2
  3  4  5  6  7  8  9
 10 11 12 13 14 15 16
 17 18 19 20 21 22 23
 24 25 26 27 28 29 30
 31                  

          4          
 Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6
  7  8  9 10 11 12 13
 14 15 16 17 18 19 20
 21 22 23 24 25 26 27
 28 29 30            

          5          
 Su Mo Tu We Th Fr Sa
           1  2  3  4
  5  6  7  8  9 10 11
 12 13 14 15 16 17 18
 19 20 21 22 23 24 25
 26 27 28 29 30 31   

          6          
 Su Mo Tu We Th Fr Sa
                    1
  2  3  4  5  6  7  8
  9 10 11 12 13 14 15
 16 17 18 19 20 21 22
 23 24 25 26 27 28 29
 30                  

          7          
 Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6
  7  8  9 10 11 12 13
 14 15 16 17 18 19 20
 21 22 23 24 25 26 27
 28 29 30 31         

          8          
 Su Mo Tu We Th Fr Sa
              1  2  3
  4  5  6  7  8  9 10
 11 12 13 14 15 16 17
 18 19 20 21 22 23 24
 25 26 27 28 29 30 31

          9          
 Su Mo Tu We Th Fr Sa
  1  2  3  4  5  6  7
  8  9 10 11 12 13 14
 15 16 17 18 19 20 21
 22 23 24 25 26 27 28
 29 30               

         10          
 Su Mo Tu We Th Fr Sa
        1  2  3  4  5
  6  7  8  9 10 11 12
 13 14 15 16 17 18 19
 20 21 22 23 24 25 26
 27 28 29 30 31      

         11          
 Su Mo Tu We Th Fr Sa
                 1  2
  3  4  5  6  7  8  9
 10 11 12 13 14 15 16
 17 18 19 20 21 22 23
 24 25 26 27 28 29 30

         12          
 Su Mo Tu We Th Fr Sa
  1  2  3  4  5  6  7
  8  9 10 11 12 13 14
 15 16 17 18 19 20 21
 22 23 24 25 26 27 28
 29 30 31            

$
$ ./a.out
Annual calendar will be shown.
Which day of the week is Jan 1?
Input figure among following figures.
0:Sun, 1:Mon, 2:Tue, 3:Wed, 4:Thu, 5:Fri, 6:Sat
1
If the year is leap-year, input 1. Otherwise, input 0.
1
Input number of months shown in a row.
2

          1                      2          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6                1  2  3
  7  8  9 10 11 12 13    4  5  6  7  8  9 10
 14 15 16 17 18 19 20   11 12 13 14 15 16 17
 21 22 23 24 25 26 27   18 19 20 21 22 23 24
 28 29 30 31            25 26 27 28 29      

          3                      4          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                 1  2       1  2  3  4  5  6
  3  4  5  6  7  8  9    7  8  9 10 11 12 13
 10 11 12 13 14 15 16   14 15 16 17 18 19 20
 17 18 19 20 21 22 23   21 22 23 24 25 26 27
 24 25 26 27 28 29 30   28 29 30            
 31                                         

          5                      6          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
           1  2  3  4                      1
  5  6  7  8  9 10 11    2  3  4  5  6  7  8
 12 13 14 15 16 17 18    9 10 11 12 13 14 15
 19 20 21 22 23 24 25   16 17 18 19 20 21 22
 26 27 28 29 30 31      23 24 25 26 27 28 29
                        30                  

          7                      8          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6                1  2  3
  7  8  9 10 11 12 13    4  5  6  7  8  9 10
 14 15 16 17 18 19 20   11 12 13 14 15 16 17
 21 22 23 24 25 26 27   18 19 20 21 22 23 24
 28 29 30 31            25 26 27 28 29 30 31

          9                     10          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
  1  2  3  4  5  6  7          1  2  3  4  5
  8  9 10 11 12 13 14    6  7  8  9 10 11 12
 15 16 17 18 19 20 21   13 14 15 16 17 18 19
 22 23 24 25 26 27 28   20 21 22 23 24 25 26
 29 30                  27 28 29 30 31      

         11                     12          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                 1  2    1  2  3  4  5  6  7
  3  4  5  6  7  8  9    8  9 10 11 12 13 14
 10 11 12 13 14 15 16   15 16 17 18 19 20 21
 17 18 19 20 21 22 23   22 23 24 25 26 27 28
 24 25 26 27 28 29 30   29 30 31            

$
$ ./a.out
Annual calendar will be shown.
Which day of the week is Jan 1?
Input figure among following figures.
0:Sun, 1:Mon, 2:Tue, 3:Wed, 4:Thu, 5:Fri, 6:Sat
1
If the year is leap-year, input 1. Otherwise, input 0.
1
Input number of months shown in a row.
5

          1                      2                      3          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6                1  2  3                   1  2
  7  8  9 10 11 12 13    4  5  6  7  8  9 10    3  4  5  6  7  8  9
 14 15 16 17 18 19 20   11 12 13 14 15 16 17   10 11 12 13 14 15 16
 21 22 23 24 25 26 27   18 19 20 21 22 23 24   17 18 19 20 21 22 23
 28 29 30 31            25 26 27 28 29         24 25 26 27 28 29 30
                                               31                  

          4                      5                      6          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6             1  2  3  4                      1
  7  8  9 10 11 12 13    5  6  7  8  9 10 11    2  3  4  5  6  7  8
 14 15 16 17 18 19 20   12 13 14 15 16 17 18    9 10 11 12 13 14 15
 21 22 23 24 25 26 27   19 20 21 22 23 24 25   16 17 18 19 20 21 22
 28 29 30               26 27 28 29 30 31      23 24 25 26 27 28 29
                                               30                  

          7                      8                      9          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6                1  2  3    1  2  3  4  5  6  7
  7  8  9 10 11 12 13    4  5  6  7  8  9 10    8  9 10 11 12 13 14
 14 15 16 17 18 19 20   11 12 13 14 15 16 17   15 16 17 18 19 20 21
 21 22 23 24 25 26 27   18 19 20 21 22 23 24   22 23 24 25 26 27 28
 28 29 30 31            25 26 27 28 29 30 31   29 30               

         10                     11                     12          
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
        1  2  3  4  5                   1  2    1  2  3  4  5  6  7
  6  7  8  9 10 11 12    3  4  5  6  7  8  9    8  9 10 11 12 13 14
 13 14 15 16 17 18 19   10 11 12 13 14 15 16   15 16 17 18 19 20 21
 20 21 22 23 24 25 26   17 18 19 20 21 22 23   22 23 24 25 26 27 28
 27 28 29 30 31         24 25 26 27 28 29 30   29 30 31            

$

Lessons for C programming
Junichi Susaki, Kyoto University