Professional Documents
Culture Documents
EXERCISE 1
Let's look at a simple program for you to try out on your own.
1
2 #include <stdio.h>
3
4 int main() /* Most important part of the program! */
5 {
6 int age; /* Need a variable... */
7
8 printf( "Please enter your age: " ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
9 if ( age < 100 ) { /* If the age is less than 100 */
10 printf ("You are pretty young!\n" ); /* Just to show you it works... */
11 }
12 else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are old\n" );
13 }
14 else {
15 printf( "You are really old\n" ); /* Executed if no other statement is */
16 }
return 0;
17
18 }
19
EXERCISE 2
Print "Hello World" if x is greater than y.
int x = 50;
int y = 10;
if >
(x y) {
printf("Hello World");
}
IF >
Examples
FIS 1084
EXERCISE 3
int x = 50;
int y = 50;
if ==
(x y) {
printf("Yes");
else
} {
printf("No");
}
IF == else
EXERCISE 4
Print "1" if x is equal to y, print "2" if x is greater than y, otherwise
print "3".
int x = 50;
int y = 50;
if ==
(x y) {
printf("1");
else >
} (x y) {
printf("2");
if
} {
printf("3");
}
==
Examples
FIS 1084
If ==
Else if >
else
EXERCISE 5
#include <stdio.h>
int main() {
if (20 > 18) {
printf("20 is greater than 18");
}
return 0;
}
EXERCISE 6
#include <stdio.h>
int main() {
int time = 20;
if (time < 18) {
printf("Good day.");
} else {
printf("Good evening.");
}
return 0;
}
Examples
FIS 1084
EXERCISE 7
#include <stdio.h>
int main() {
int myNum = 10;
if (myNum > 0)
printf("The value is a positive number.");
else if (myNum < 0)
printf("The value is a negative number.");
else
printf("The value is 0.");
return 0;
}
Examples