If-statement

Exercise 1

Write a below source code and save it as "if1.c". After compilation, confirm by executing it with different input data.

#include <stdio.h>

int main(void) {

    int x;

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

    if (x > 0){ 
        printf("It is positive number.\n");
    } else if (x < 0){
        printf("It is negative number.\n");
    } else {
        printf("It is 0.\n");
    }

    return (0);
}
(Execution result)
$ ./a.out
Input integer  -9
It is negative number.
$
$ ./a.out
Input integer 8
It is positive number.
$
$ ./a.out
Input integer 0
It is 0.
$

Exercise 2

A source code is shown to compare two input figures, 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 a, b;

    printf("Input two figures ");
    scanf("%d%d", &a,&b);

    if (*********) {
    	printf("%d is greater than %d.\n", a, b);
    } else if (*********) {
    	printf("%d is greater than %d.\n", b, a);
    } else {
    	printf("Both are equal.\n");
    }

    return (0);
}
(Execution result)
$ ./a.out
Input two figures 10 20
20 is greater than 10.
$
$ ./a.out
Input two figures 30 10
30 is greater than 10.
$
$ ./a.out
Input two figures 15 15
Both are equal.
$

Exercise 3

A source code is shown to change message based on birthday's month, but some parts of the code are hidden by "******". Write the source code, complement the expressions hidden by "******" and save it as "if3.c". After compilation, confirm by executing it with different input data.

Four seasons are classified as follows.

Spring March(3), April(4), May(5)
Summer June(6), July(7), August(8)
Autumn September(9), October(10), November(11)
Winter December(12), January(1), February(2)
#include <stdio.h>

int main(void) {

    int a;

    printf("Input your birthday's month by integer ");
    scanf("%d", &a);

    if (************************) {
    	printf("Your birth month is spring\n");
    } else if (************************) {
    	printf("Your birth month is summer\n");
    } else if (************************) {
    	printf("Your birth month is autumn\n");
    } else if (************************) {
    	printf("Your birth month is winter\n");
    } else {
    	printf("Input is not integer from 1 to 12\n");
    }

    return (0);
}
(Execution result)
$ ./a.out
Input your birth month by integer 12
Your birth month is winter
$
$ ./a.out
Input your birth month by integer 8
Your birth month is summer
$
$ ./a.out
Input your birth month by integer 13
Input is not integer from 1 to 12
$

Excercise 4

You want to show grade based on input score of examination. In order to realize such function, program and save the source code as "if4.c". After compilation, confirm by executing it with different input data.

Four seasons are classified as follows.

Range of score (x) Grade
80 <= x <= 100 A
70 <= x < 80 B
60 <= x < 70 C
0 <= x < 60 D
Others Error message
(Execution result)
$ ./a.out
Input your score 92
Your grade is A.
$
$ ./a.out
Input your score 59
Your grade is D.
$
$ ./a.out
Input your score 130
Input error !

Excercise 5

As a below table shows, discount ratio depends on purchase amount of money. Considering this discount ratio, program and save as the source code as if5.c to show discounted amount of money.

Purchase amount (JPY) Discount ratio
x < 5000 0 %
5000 <= x < 10000 10 %
x >= 10000 20 %
(Execution result)
$ ./a.out
Input purchase amount of money (JPY) 3000
Total amount = JPY 3000
$
$ ./a.out
Input purchase amount of money (JPY) 9999
Discount = JPY 999
Total amount = JPY 9000
$
$ ./a.out
Input purchase amount of money (JPY) 6000
Discount = JPY 600
Total amount = JPY 5400
$
$
$ ./a.out
Input purchase amount of money (JPY) 15000
Discount = JPY 3000
Total amount = JPY 12000
$
$ ./a.out
Input purchase amount of money (JPY) 19999
Discount = JPY 3999
Total amount = JPY 16000
$
$ ./a.out
Input purchase amount of money (JPY) -1000
Input proper figure

Exercise 6

Program to show whether input integer is dividable by 2 and 3, and save the source code as "if6.c".

(Execution result)
$ ./a.out
Input integer 14
14 is dividable by 2, but not by 3.
$
$ ./a.out
Input integer 30
30 is dividable by 2 and 3.
$
$ ./a.out
Input integer 17
17 is not dividable by 2 or 3
$
$ ./a.out
Input integer 15
15 is dividable by 3, but not by 2.

Exercise 7

Program to determine whether this year birthday has passed or not, and save the source code as "if7.c". Input data are (1) birthday's month, (2) birthday's day, (3) today's month, and (4) today's day.

(Execution result)
$ ./a.out
Input your birthday's month and day
3 2
Input today's month and day
2 1
Your birthday has not passed.
$
$ ./a.out
Input your birthday's month and day
3 2
Input today's month and day
3 2
Today is your birthday !
$
$ ./a.out
Input your birthday's month and day
3 2
Input today's month and day
12 20
Your birthday has passed.

Lessons for C programming
Junichi Susaki, Kyoto University