0% found this document useful (0 votes)
10 views15 pages

Saurav 'S Lab Work 02

This lab report from Triton International College focuses on control structures in C programming, including if, if-else, else-if ladder, nested if-else, and switch-case. It provides theoretical explanations and practical examples to demonstrate the implementation of these structures. The conclusion emphasizes the importance of these control structures in programming for managing execution flow based on conditions.

Uploaded by

titixe9032
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

Saurav 'S Lab Work 02

This lab report from Triton International College focuses on control structures in C programming, including if, if-else, else-if ladder, nested if-else, and switch-case. It provides theoretical explanations and practical examples to demonstrate the implementation of these structures. The conclusion emphasizes the importance of these control structures in programming for managing execution flow based on conditions.

Uploaded by

titixe9032
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

TRITON INTERNATIONAL

COLLEGE
Balkumari,kathmandu

Lab Report Of Computer Science

on C Programming

Lab Report No: 02

Submitted By Submitted To

Sau Computer Science Department

Class - xi Triton SS and College

Section- 504
Roll no:-36

Lab 2 : C Programming - Control

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,

and switch-case. We will solve several problems using these structures to


understand their practical applications.

Theory

1. If Statement

The if statement is used to execute a block of code only if a specified

condition is true. if (condition) {

// Code to execute if condition is true


}

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) {

// Code to execute if condition is true

} else {

// Code to execute if condition is false

3. Else-If Ladder

The else-if ladder is used when multiple conditions need

to be checked. if (condition1) {

// Code to execute if condition1 is true


} else if (condition2) {

// Code to execute if condition2 is true

} else {

// Code to execute if all conditions are false

}
4. Nested If-Else

Nested if-else statements are used when a condition needs to be checked

within another condition. if (condition1) {

if (condition2) {
// Code to execute if both conditions are true

} else {

// Code to execute if condition1 is true but condition2 is false

} else {

// Code to execute if condition1 is false

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:

// Code to execute if expression equals


constant1 break;

case constant2:
// Code to execute if expression equals
constant2 break;

default:
// Code to execute if expression doesn't match any case

}
Programs

1. Check if a Number is Even or Odd

#include<stdio.h
>

int main() {

int num;

printf("Enter a
number: ");
scanf("%d", &num);

if (num % 2 == 0) {

printf("%d is even.\n", num);

} else {

printf("%d is odd.\n", num);

return 0;

Output:

&
2. Check if Two Numbers are Same

#include<stdio.h
>

int main() {

int num1, num2;

printf("Enter two numbers:


"); scanf("%d %d", &num1,
&num2); if (num1 ==
num2) {

printf("The numbers are same.\n");

} else {

printf("The numbers are different.\n");

return 0;

Output:

&
3. Print Name of Days Using Switch Case

#include<stdio.h
>

int main() {

int day;

printf("Enter a number (1-7): ");


scanf("%d", &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;

printf("Enter a number: ");

scanf("%d", &num);

if (num % 4 == 0 && num % 8 == 0) {

printf("%d is divisible by both 4 and 8.\n", num);

else {

printf("%d is not divisible by both 4 and 8.\n", num);

return 0;

Output:

&
5. Check Voting Eligibility

#include
<stdio.h>

int main() {

int age;

printf("Enter your age:


"); scanf("%d", &age);

if (age >= 18) {

printf("You are eligible to vote.\n");

} else {

printf("You are not eligible to vote.\n");

return 0;

Output:
6. Calculate Profit or Loss

#include
<stdio.h>
int main() {

float costPrice, sellingPrice;

printf("Enter cost price and selling price:


"); scanf("%f %f", &costPrice,
&sellingPrice);

if (sellingPrice > costPrice)

printf("Profit: %.2f\n", sellingPrice - costPrice);

} else if (sellingPrice < costPrice)

printf("Loss: %.2f\n", costPrice - sellingPrice);

} else {

printf("No profit, no loss.\n");

return 0;

Output:
7. Find Greatest Among Three Numbers

#include
<stdio.h> int
main() {

int num1, num2, num3;


printf("Enter three numbers:
");

scanf("%d %d %d", &num1, &num2,


&num3); if (num1 >= num2 && num1
>= num3)

printf("%d is the greatest.\n", num1);

} else if (num2 >= num1 && num2 >=


num3)

printf("%d is the greatest.\n", num2);

} else

printf("%d is the greatest.\n", num3);

return 0;

Output:
8. Find Smallest Among Three Numbers

#include
<stdio.h> int
main() {

int num1, num2, num3;


printf("Enter three numbers:
");

scanf("%d %d %d", &num1, &num2, &num3);

if (num1 <= num2 && num1 <=


num3)

printf("%d is the smallest.\n",


num1);

} else if (num2 <= num1 && num2 <=


num3)

printf("%d is the smallest.\n", num2);

} else {

printf("%d is the smallest.\n", num3);

return 0;

}
Output:

9. Calculate Employee Salary

#include
<stdio.h> int
main() {

int hours;

float salary;

printf("Enter working hours: ");


scanf("%d", &hours);

if (hours <= 8) {

salary = hours * 100;

} else {

salary = 8 * 100 + (hours - 8) * 120;

printf("Total salary: %.2f\n",


salary); return 0;

Output:
10. Classify Age

#include
<stdio.h> int
main() {

int age;

printf("Enter your age:


"); scanf("%d", &age);

if (age < 13)

printf("Child\n");

else if (age >= 13 && age <


20)

printf("Teenager\n");

} else {

printf("Adult\n");

return 0;

}
Output:

11. Calculate Commission

#include
<stdio.h> int
main() {

float amount, commission;


printf("Enter the amount
sold: "); scanf("%f",
&amount);

if (amount <= 20000)

{ commission = amount *
0.05;

else if (amount > 20000 && amount <=


50000)

{ commission = amount * 0.15;

} else

commission = amount * 0.25;

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

You might also like