Lab 4
Lab 4
These conditions are specified by a set of conditional statements having boolean expressions
which are evaluated to a boolean value true or false. This process is called decision making in
‘C.’
If Statement:
The if statement is used to check some given condition and perform some operations
depending upon the correctness of that condition. It is mostly used in the scenario where we
need to perform the different operations for the different conditions. The syntax of the if
statement is given below:
1. if(expression){
2. //code to be executed
3. }
If-else Statement:
The if-else statement is used to perform two operations for a single condition. The if-else
statement is an extension to the if statement using which, we can perform two different
operations, i.e., one is for the correctness of that condition, and the other is for the
incorrectness of the condition. Here, we must notice that if and else block cannot be executed
simultaneously. Using if-else statement is always preferable since it always invokes an
otherwise case with every if condition. The syntax of the if-else statement is given below:
1. if(expression){
2. //code to be executed if condition is true
3. }else{
4. //code to be executed if condition is false
5. }
1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }
Nested If Statement:
Nested if statement in C is the nesting of if statement within another if statement and nesting
of if statement with an else statement. Once an else statement gets failed there are times when
the next execution of statement wants to return a true statement, there we need nesting of if
statement to make the entire flow of code in a semantic order. Nested if statement in C plays a
very pivotal role in making a check on the inner nested statement of if statement with an else
very carefully. The syntax of nested if statement is given below:
}
// if the first condition does not hold
else{
Level: 1
1. What would be the output of:
#include <stdio.h>
int main(){ int a =
5;
int b,c; if
(a==4){
b = 6;
c = 7;
}
printf("%d %d\n",b,c);
}
2. What would be the value of:
'A'>'a'
#include <stdio.h>
int main(){ if (1){
printf("Hello\n");
}
}
#include <stdio.h>
int main(){
if(1){
if(2>4 && 5>3){
printf("COOL\
n"); }
}
}
Level: 2
1. Take values of length and breadth of a rectangle from user and check if it is square or not.
2. Take two int values from user and print greatest among them.
3. A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
Ask user for quantity Suppose,
one unit will cost 100.
Judge and print total cost for user.
4. A company decided to give bonus of 5% to employee if his/her year of service is more
than 5 years.
Ask user for their salary and year of service and print the net bonus amount.
9. A student will not be allowed to sit in exam if his/her attendence is less than 75%.
Take following input from
user Number of classes held
Number of classes attended.
And print percentage of class
attended
Is student is allowed to sit in exam or not.
10. Modify the above question to allow student to sit if he/she has medical cause. Ask
user if he/she has medical cause or not ( 'Y' or 'N' ) and print accordingly.
Level: 3
1. Write a C program to check if a year is leap year or not. If a year is divisible by 4
then it is leap year but if the year is century year like 2000, 1900, 2100 then it must be
divisible by 400.
2. A 4 digit number is entered through keyboard. Write a C program to print a new
number with digits reversed as of orignal one. E.g.-
INPUT : 1234 OUTPUT : 4321
INPUT : 5982 OUTPUT : 2895