Lab Report Cse
Lab Report Cse
Experiment No: 1
Name of the Experiment: Arithmetic Calculation
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
Exercise1:
Theory:
In C programming, we can determine if a number is even or odd using bitwise operations. In the binary
representation of numbers, even numbers have their least significant bit (rightmost bit) as 0, while odd numbers
have it as 1.
Using the bitwise AND operator (&), we can efficiently check this:
we can also check if a number is even or odd using the modulus operator (%). An integer num is
even if num % 2 == 0 (remainder is zero) and odd if num % 2 == 1 (remainder is one). This
approach is simple and commonly used for checking even or odd numbers.
Source Code:
Using Bitwise operator
#include <stdio.h>
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);
if(num & 1)
{
printf("%d is odd.", num);
}
else
{
printf("%d is even.", num);
}
return 0;
}
Using modular operator
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
Output:
Discussions:
(i) The program checks whether the number is odd or even.
(ii) Firstly, I have checked using bitwise and then using modular operator
(iii) In the program, I used the proper method for checking odd or even using modular and
bitwise operator.
Exercise 2: Write a program that reads any angle and display cos, tan, sec and cosec.
Theory:
A C program reads an angle, converts it to radians, and calculates:
cos(x): Cosine.
tan(x): Tangent (sin(x) / cos(x)).
sec(x): Reciprocal of cosine (1 / cos(x)).
cosec(x): Reciprocal of sine (1 / sin(x)).
Functions from <math.h> are used. Degrees are converted to radians using:
Radians = degree*(pi/180)
Edge cases: Handle division by zero for sec and cosec.
Source code:
#include <stdio.h>
#include <math.h>
#define PI 3.1416
int main() {
double angle, radians;
double cosine, tangent, secant, cosecant;
cosine = cos(radians);
tangent = tan(radians);
if (cosine != 0)
secant = 1 / cosine;
else
printf("Secant (sec) is undefined for this angle.\n");
if (sin(radians) != 0)
cosecant = 1 / sin(radians);
else
printf("Cosecant (cosec) is undefined for this angle.\n");
if (sin(radians) != 0)
printf("Cosecant (cosec): %.4lf\n", cosecant);
return 0;
}
Output:
Discussion:
1. This program reads any angle and display cos, tan, sec and cosec.
2. First this program takes angles as degrees and then using formula converts it into radian.
3. After converting into radian it shows the cos tan sec and cosec value of that angle.
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;
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.