LAB EXERCISE #3
C if Statement
The syntax of the if statement in C programming is:
if (test expression)
{
// code
}
The if statement evaluates the test expression inside the parenthesis ().
If the test expression is evaluated to true, statements inside the body of if are
executed.
If the test expression is evaluated to false, statements inside the body of if are
not executed.
Example 1: if statement
// Program to display a number if it is negative
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}
Output 1
Enter an integer: -2
You entered -2.
The if statement is easy.
When the user enters -2, the test expression number<0 is evaluated to true.
Hence, You entered -2 is displayed on the screen.
Output 2
Enter an integer: 5
The if statement is easy.
When the user enters 5, the test expression number<0 is evaluated to false and
the statement inside the body of if is not executed
C if...else Statement
The if statement may have an optional else block. The syntax of the
if..else statement is:
if (test expression) {
// run code if test expression is true
}
else {
// run code if test expression is false
}
If the test expression is evaluated to true,
statements inside the body of if are executed.
statements inside the body of else are skipped from execution.
If the test expression is evaluated to false,
statements inside the body of else are executed
statements inside the body of if are skipped from execution.
Example 2: if...else statement
// Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
Run Code
Output
Enter an integer: 7
7 is an odd integer.
When the user enters 7, the test expression number%2==0 is evaluated to false.
Hence, the statement inside the body of else is executed.
C if...else Ladder
The if...else statement executes two different codes depending upon
whether the test expression is true or false. Sometimes, a choice has to be made
from more than 2 possibilities. The if...else ladder allows you to check between
multiple test expressions and execute different statements.
Syntax of if...else Ladder
if (test expression1) {
// statement(s)
}
else if(test expression2) {
// statement(s)
}
.
.
else {
// statement(s)
}
Example 3: C if...else Ladder
// Program to relate two integers using =, > or < symbol
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}
//checks if both test expressions are false
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
Output
Enter two integers: 12
23
Result: 12 < 23
Nested if...else
It is possible to include an if..else statement inside body of another
if..else statement.
Example 4: Nested if...else
This program given below relates two integers using either <, > and = similar to
the if...else ladder's example. However, we will use a nested if...else
statement to solve this problem.
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2) {
if (number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
else {
printf("Result: %d > %d", number1, number2);
}
}
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
If the body of an if...else statement has only one statement, you do not need
to use brackets {}. For example, this code
if (a > b) {
printf("Hello");
}
printf("Hi");
Program: Write a program to print whether a given number is even or odd.
Code: (Use comments wherever applicable)
#include<stdio.h>
void main()
{
int num;
printf("Enter the
number: ");
scanf(“%d”,&num);
if(num%2==0)
printf(“\n %d is even”, num);
else
printf(“\n %d is odd”, num);
getch();
}
Output:
Enter the number: 6 6 is even
SAMPLE PROGRAMS
(Students are to code the following programs in the lab and show the
output to instructor/course Teacher)
Instructions
Write comment to make your programs readable.
Use descriptive variables in your programs(Name of the
variables should show their purposes)
Programs List
Write a program to compute grade of students using if else ladder. The
grades are assigned as followed:
a. MarksGrade
b. marks<50 F
c. 50≤marks< 60 C
d. 60≤marks<70 B
e. 70≤marks<80 B+
f. 80≤marks<90 A
g. 90≤mars≤ 100 A+
Program: Write a program to find whether a character is consonant or vowel using
switch statement.
#include <stdio.h> void
main()
{
char ch;
printf(“Enter any alphabet:”); //input alphabet from user
scanf(“%c”, &ch);
switch(ch)
{
case „a :
case „A :
printf(“Vowel”);
break;
case „e :
case „E :
printf(“Vowel”);
break;
case „i :
case „I :
printf(“Vowel”);
break;
case „o :
case „O :
printf(“Vowel”);
break;
case „u :
case „U :
printf(“Vowel”);
break;
default:
printf(“Consonant”);
}
}
Home-task
1. Write a program to determine whether the input character is capital or small letter,
digits or special symbol.
2. Write a program to check whether a date is valid or not.
3. Write a program to check whether a number is positive, negative or zero using
switch case.
4. Write a program to check whether the entered year is leap year or not (a year is
leap if it is divisible by 4 and divisible by 100 or 400.)
5. Write a C program to find maximum between two numbers.
6. Write a program to print day name using switch case.
7. Write a Program to Check Whether a Number is Prime or not.
8. Some operators checks about the relationship between two values and these operators are
called relational operators. Given two numerical values your job is just to find out the
relationship between them that is (i) First one is greater than the second (ii) First one is less
than the second or (iii) First and second one is equal.
Input
First line of the input file is an integer t (t < 15) which denotes how many sets of inputs are
there. Each of the next t lines contain two integers a and b (|a|, |b| < 1000000001).
Output
For each line of input produce one line of output. This line contains any one of the
relational operators ‘>’, ‘< > =
Sample Input
3 10
20 20
10 10 10
Sample Output
<
>
=
9. Company XYZ have been badly hit by recession and is taking a lot of cost cutting
measures. Some of these measures include giving up office space, going open source,
reducing incentives, cutting on luxuries and issuing pink slips. They have got three (3)
employees working in the accounts department and are going to lay-off two (2) of them.
After a series of meetings, they have decided to dislodge the person who gets the most
salary and the one who gets the least. This is usually the general trend during crisis like
this. You will be given the salaries of these 3 employees working in the accounts
department. You have to find out the salary of the person who survives.
Input
The first line of input is an integer T (T < 20) that indicates the number of test cases. Each case
consists of a line with 3 distinct positive integers. These 3 integers represent the salaries of the
three employees. All these integers will be in the range [1000, 10000].
Output
For each case, output the case number followed by the salary of the person who survives.
Sample Input
3
1000 2000 3000
3000 2500 1500
1500 1200 1800
Sample Output
Case 1: 2000
Case 2: 2500
Case 3: 1500
10. Write a C program where user can input electricity unit used and calculate total
electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill