Cse PDF
Cse PDF
Experiment No: 1
Name of the Experiment: Selection Structure(If-else & switch) and conditional Operator
Submitted To:
Utsha Das
Lecturer
Submitted By:
Name: Md. Tasnim Alam Turzo
Theory: A leap year is a year divisible by 4 but not by 100, except years divisible by 400, which
are also leap years. The program takes a year as input and uses if-else conditions to check these
rules. It first checks if the year is divisible by 400 (leap year), then checks if it is divisible by 4
but not 100 (leap year), otherwise, it declares the year is not a leap year.
Source Code:
#include <stdio.h>
int main()
int year;
Else
return 0;
Output:
Discussions:
Exercise 2: Write a program that three numbers (a,b,c) and determine the roots of
quadratic equation ax2+bx+c = 0.
The term b2−4acb^2 - 4acb2−4ac (discriminant) determines the nature of the roots:
Program Steps:
4. Find Roots:
source code:
#include <stdio.h>
#include <math.h>
int main() {
scanf("%lf", &a);
scanf("%lf", &b);
scanf("%lf", &c);
if (a == 0) {
else {
discriminant = b * b - 4 * a * c;}
if (discriminant > 0) {
printf("The equation has two distinct real roots: %.2lf and %.2lf\n", root1, root2);}
else if (discriminant == 0) {
root1 = -b / (2 * a);
else {
realPart = -b / (2 * a);
return 0;
}
Output:
Discussion:
1. The program takes 3 numbers as input and checks them.
2. The the program determines the discriminant and according to that determines the root
3. The program uses the exact formula to determine the roots.
Exercise 3: Write a program that read numbers and display medium using conditional
operator.
Theory: The program determines the median of three numbers using the conditional
operator. The median is the middle value in a sorted list of three numbers. The program
takes three inputs, compares the numbers using nested conditional operators, and
identifies the value that is neither the smallest nor the largest. The result is then displayed.
This approach is concise and avoids complex if-else structures.
Source code:
int main() {
int num1, num2, num3, median;
return 0;
}
Output:
Discussion:
1. The program reads three number and displays the median.
2. The program uses ternary operator rather than using if-else statement.
Exercise 4: Write a program that read a mark and display its grade using
switch statement.
Theory: The program reads a student's mark and uses a switch statement to display the
corresponding grade. The mark is divided by 10 to group it into ranges (e.g., 90–100 as 9 or
10). Each case in the switch represents a grade range, and the default case handles failing
marks. This approach simplifies grade classification and ensures clear, structured output.
Source code:
#include <stdio.h>
int main() {
int mark, grade;
switch (grade) {
case 10: printf("Grade: A\n"); break;
case 9: printf("Grade: A\n"); break;
case 8: printf("Grade: B\n"); break;
case 7: printf("Grade: C\n"); break;
case 6: printf("Grade: D\n"); break;
case 5: printf("Grade: E\n"); break;
default: printf("Grade: F\n"); break; // For grades below 50
}
}
return 0;
}
Output:
Discussion:
1. The program uses switch statement to show the grades .
2. First the program takes numbers as input and checks whether there is an error. After that
it shows grades according to numbers.
Exercise 5: Write a program that read any number and display equivalent roman number
using switch.
Theory: The program reads a number (typically between 1 and 10) and converts it into its
equivalent Roman numeral using a switch statement. Each case in the switch corresponds
to a specific number and outputs its Roman numeral representation. If the input is outside
the valid range, the program displays an error message. This approach ensures efficient
and straightforward mapping of numbers to Roman numerals.
Source code:
#include <stdio.h>
int main() {
int number;
switch (number) {
case 1: printf("Roman numeral: I\n"); break;
case 2: printf("Roman numeral: II\n"); break;
case 3: printf("Roman numeral: III\n"); break;
case 4: printf("Roman numeral: IV\n"); break;
case 5: printf("Roman numeral: V\n"); break;
case 6: printf("Roman numeral: VI\n"); break;
case 7: printf("Roman numeral: VII\n"); break;
case 8: printf("Roman numeral: VIII\n"); break;
case 9: printf("Roman numeral: IX\n"); break;
case 10: printf("Roman numeral: X\n"); break;
default: printf("Invalid number. Please enter a number between 1 and 10.\n");
}
return 0;
}
Output
Discussion:
1. This program uses switch statement to print roman numbers.
2. This approach ensures efficient and straightforward mapping of numbers to Roman
numerals.