Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

New Cse Lab Report

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Heaven`s Light is Our Guide

Rajshahi University of Engineering &Technology


Department of Computer Science and Engineering

Experiment No: 1
Name of the Experiment: Selection Structure(If-else & switch) and conditional Operator
Submitted To:
Utsha Das
Lecturer
Department of Computer Science and Engineering
Rajshahi University of Engineering & Technology
Submitted By:
Name: Md. Tasnim Alam Turzo
Roll No: 2301030
Class: 1st Year Odd Semester.
 Machine Configuration: ASUS 16 A1605ZA, Intel® Core™ i5-12500H Processor,16GB
DDR4 RAM,512 GB NVME SSD,64-bit operating system
Exercise 1:Write a program that read any year and display leap year or not.
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;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{ printf("%d is a leap year.\n", year); }
Else
{ printf("%d is not a leap year.\n", year); }
return 0;
}

Output:
Discussions:
1.The program takes year as input and checks it.
2.Then it uses AND operation and OR operation to check leap year.
3.The program uses the exact formula to check whether a year is leap year or not.

Exercise 2: Write a program that three numbers (a,b,c) and determine the roots of
quadratic equation ax2+bx+c = 0.
Theory: A quadratic equation ax2+bx+c= is solved using the quadratic formula:

The term b2−4acb^2 - 4acb2−4ac (discriminant) determines the nature of the roots:

1. D>0: Two distinct real roots.


2. D=0: One repeated real root.
3. D<0: Two complex roots.
Program Steps:

1. Input: Read a, b, c from the user.


2. Check: If a=0, it's not a quadratic equation.
3. Compute Discriminant: D=b2−4ac
4. Find Roots:
o For D>0, calculate two real roots.

o For D=0, calculate one repeated root.

o For D<0, calculate two complex roots.

source code:

#include <stdio.h>

#include <math.h>

int main() {

double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;

printf("Enter coefficient a: ");

scanf("%lf", &a);

printf("Enter coefficient b: ");

scanf("%lf", &b);

printf("Enter coefficient c: ");

scanf("%lf", &c);

if (a == 0) {

printf("This is not a quadratic equation.\n");}

else {

discriminant = b * b - 4 * a * c;}
if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("The equation has two distinct real roots: %.2lf and %.2lf\n", root1, root2);}

else if (discriminant == 0) {

root1 = -b / (2 * a);

printf("The equation has one real root (repeated): %.2lf\n", root1);}

else {

realPart = -b / (2 * a);

imaginaryPart = sqrt(-discriminant) / (2 * a);

printf("The equation has two complex roots: %.2lf+%.2lfi and %.2lf-%.2lfi\n",

realPart, imaginaryPart, realPart, imaginaryPart);

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;

printf("Enter three numbers: ");


scanf("%d %d %d", &num1, &num2, &num3);
median = (num1 > num2)
? ((num2 > num3) ? num2 : (num1 > num3 ? num3 : num1))
: ((num1 > num3) ? num1 : (num2 > num3 ? num3 : num2));
printf("The median is: %d\n", 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;

printf("Enter the mark (0-100): ");


scanf("%d", &mark);
if (mark < 0 || mark > 100) {
printf("Invalid mark. Please enter a value between 0 and 100.\n");
} else {
grade = mark / 10;

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;

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


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

You might also like