Saurav 'S Lab Work 02
Saurav 'S Lab Work 02
COLLEGE
Balkumari,kathmandu
on C Programming
Submitted By Submitted To
Section- 504
Roll no:-36
Structures Introduction
Control structures in C programming are used to control the flow of execution based
on certain conditions or loops. This lab focuses on various control structures such as
if, if-else, else-if ladder, nested if-else,
Theory
1. If Statement
2. If-Else Statement
The if-else statement allows you to execute one block of code if the condition is true
and another block if the condition is false.
if (condition) {
} else {
3. Else-If Ladder
to be checked. if (condition1) {
} else {
}
4. Nested If-Else
if (condition2) {
// Code to execute if both conditions are true
} else {
} else {
5. Switch Case
The switch-case statement is used to execute one block of code among many based
on the value of a variable.
switch
(expression)
{ case
constant1:
case constant2:
// Code to execute if expression equals
constant2 break;
default:
// Code to execute if expression doesn't match any case
}
Programs
#include<stdio.h
>
int main() {
int num;
printf("Enter a
number: ");
scanf("%d", &num);
if (num % 2 == 0) {
} else {
return 0;
Output:
&
2. Check if Two Numbers are Same
#include<stdio.h
>
int main() {
} else {
return 0;
Output:
&
3. Print Name of Days Using Switch Case
#include<stdio.h
>
int main() {
int day;
switch (day) {
case 1: printf("Monday\n");
break; case 2: printf("Tuesday\
n"); break; case 3:
printf("Wednesday\n");break;
case 4: printf("Thursday\n");
break; case 5: printf("Friday\
n"); break;
case 6: printf("Saturday\n");
break; case 7: printf("Sunday\
n"); break; default:
printf("Invalid day\n");
return 0;
Output:
4. Check if a Number is Divisible by 4 and 8
#include
<stdio.h>
int main() {
int num;
scanf("%d", &num);
else {
return 0;
Output:
&
5. Check Voting Eligibility
#include
<stdio.h>
int main() {
int age;
} else {
return 0;
Output:
6. Calculate Profit or Loss
#include
<stdio.h>
int main() {
} else {
return 0;
Output:
7. Find Greatest Among Three Numbers
#include
<stdio.h> int
main() {
} else
return 0;
Output:
8. Find Smallest Among Three Numbers
#include
<stdio.h> int
main() {
} else {
return 0;
}
Output:
#include
<stdio.h> int
main() {
int hours;
float salary;
if (hours <= 8) {
} else {
Output:
10. Classify Age
#include
<stdio.h> int
main() {
int age;
printf("Child\n");
printf("Teenager\n");
} else {
printf("Adult\n");
return 0;
}
Output:
#include
<stdio.h> int
main() {
{ commission = amount *
0.05;
} else
printf("Commission: %.2f\n",
commission); return 0;
}
Output:
Conclusion
This lab session helped in understanding the practical implementation of various control
structures in C programming. By solving different problems, we learned how to use if, if-else,
else-if ladder, nested if- else, and switch-case statements effectively. These structures are
fundamental in controlling the flow of programs based on conditions and are widely used in
real-world applications