0% found this document useful (0 votes)
24 views14 pages

Lab Report - 2

This CSE Lab Report presents a series of C programming exercises focused on the use of conditional statements, including eligibility for voting, ratio calculations, and checks for even/odd numbers. Each experiment includes a description, source code, and output demonstrating the application of if-else and else-if structures. The report concludes with lessons learned and common mistakes to avoid in programming.

Uploaded by

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

Lab Report - 2

This CSE Lab Report presents a series of C programming exercises focused on the use of conditional statements, including eligibility for voting, ratio calculations, and checks for even/odd numbers. Each experiment includes a description, source code, and output demonstrating the application of if-else and else-if structures. The report concludes with lessons learned and common mistakes to avoid in programming.

Uploaded by

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

CSE Lab Report-2

Submitted by:
Name: Md. Abid Al Azmain Shakil
ID: 2251081143
Batch: 63(D)
Department: CSE

Submitted to:
Name: Noor Easrib Tiba
Submission Date: 06/03/2025
Department of CSE
Uttara University
Introduction

C programming is one of the most widely used and powerful programming languages, known for
its simplicity and efficiency. It provides a variety of built-in operators, control structures, and
functions that allow developers to write programs for a variety of applications. C programming
introduces key concepts like variables, data types, loops, functions, and conditional statements
that are fundamental for developing effective algorithms. In this set of programs, we focus on
one of the most important control structures in programming: conditional statements. These
programs utilize if-else and else-if statements to implement logical decision-making in C. By the
end of this set of programs, we will have demonstrated how to use these basic constructs to
solve real-world problems.

Description

1. Eligibility for Voting: In this program, the user is asked to enter their age, and the
program determines whether they are eligible to vote based on the legal voting age (18
years). The program uses an if-else statement to check whether the age is greater than or
equal to 18, printing an appropriate message based on the result.
2. Ratio Calculation of a + b to c - d: This program takes four integers as input: a, b, c, and d.
It then calculates the ratio of a + b to c - d provided that c - d is not zero (to avoid division
by zero errors). This is achieved using an if-else statement, ensuring that the program
doesn't attempt an invalid operation, like dividing by zero.
3. Even or Odd Check: Here, the program checks whether a given number is even or odd by
using the modulus operator (%). If the number is divisible by 2 (i.e., num % 2 == 0), it is
classified as even; otherwise, it is odd. This simple check demonstrates how to use the
modulus operator in conditional expressions.
4. Divisibility by 5 and 11: This program checks whether a number is divisible by both 5 and
11 simultaneously. The program uses the modulus operator to check the divisibility of the
number by both 5 and 11. The logical && (AND) operator ensures that both conditions
are true before displaying the message that the number is divisible by both.
5. Greater Number Between Two Numbers: In this program, two integers are compared,
and the greater of the two numbers is displayed. The if-else condition checks if the first
number is greater than the second. If not, the second number is printed as the greater
one. If the numbers are equal, the program outputs that they are the same.
6. Check Whether a Number is Negative, Positive, or Zero: This program accepts a number
from the user and classifies it as either positive, negative, or zero. The program uses an if-
else if-else ladder to check each condition and display an appropriate message based on
the input.
7. Leap Year Check: This program checks whether a given year is a leap year. The logic for
determining a leap year involves two main conditions: the year must be divisible by 4, but
not divisible by 100, unless it is also divisible by 400. The program uses nested if
statements to check these conditions.
8. Find the Greatest of Three Numbers Using Nested if-else: This program demonstrates
how to find the largest number among three inputs using nested if-else statements. It
first compares two numbers and then compares the result with the third number to
determine the largest number. The nested structure ensures clarity and organization in
the decision-making process.
9. Student Grade Based on Marks Using else-if Ladder: In this program, the user enters their
marks, and the program assigns a grade based on predefined ranges. The else-if ladder
allows for multiple conditions to be checked, assigning a grade based on how well the
student performed: A for marks 90 and above, B for marks 80-89, and so on.

Experiment No. 1

Write a program in C to find whether you are eligible for voting using if-else statement.

Source Code

#include<stdio.h>
int main()
{
int age;
printf("Enter your age : ");
scanf("%d", &age);
if(age>=18)
printf("You are eligible for voting.\n");
else
printf("You are not eligible for voting.\n");
return 0;
}

Output

Experiment No. 2

Write a program in C to evaluate the ratio of a+b to c-d if c-d is not equal to 0 using if-else
statement.

Source Code
#include<stdio.h>
int main()
{
int a,b,c,d;
printf("Enter values for a, b, c, d :\n");
scanf("%d %d %d %d", &a, &b, &c, &d);
if(c-d!=0)
printf("Result:%.2f\n", (float)(a+b)/(c-d));
else
printf("Division by 0 is not allowed.\n");
return 0;
}

Output

Experiment No. 3

Write a C program to check if a number is even or odd.

Source Code

#include<stdio.h>
int main()
{
int num;
printf("Enter a number : ");
scanf("%d", &num);
if(num%2==0)
printf("%d is an even number.\n", num);
else
printf("%d is an odd number.\n", num);
return 0;
}

Output

Experiment No. 4

Write a C program to calculate if an integer number is divisible by 5 & 11.

Source Code

#include<stdio.h>
int main()
{
int num;
printf("Enter a number : ");
scanf("%d", &num);
if(num%5==0 && num%11==0)
printf("%d is divisible by both 5 and 11.\n", num);
else
printf("%d is not divisible by both 5 and 11.\n", num);
return 0;
}

Output

Experiment No. 5

Write a C program to find the greater number between two numbers.

Source Code

#include<stdio.h>
int main()
{
int a,b;
printf("Enter two numbers :\n");
scanf("%d %d", &a, &b);
if(a>b)
printf("%d is greater.\n", a);
else
printf("%d is greater.\n", b);
return 0;
}

Output

Experiment No. 6

Write a C program to check whether a number is negative, positive or zero.

Source Code

#include<stdio.h>
int main()
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
if(num>0)
printf("%d is a positive number.\n", num);
else if(num<0)
printf("%d is a negative number.\n", num);
else
printf("%d is zero.\n", num);
return 0;
}

Output

Experiment No. 7

Write a C program to check if a year is a leap year or not.

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("The year %d is a leap year.\n", year);
else
printf("The year %d is not a leap year.\n", year);
return 0;
}

Output

Experiment No. 8

Write a C program to find the greater number among three numbers by using nested if.....else
statement.

Source Code

#include <stdio.h>
int main()
{
int num1, num2, num3;
// Input the three numbers
printf("Enter three numbers :\n");
scanf("%d %d %d", &num1, &num2, &num3);
// Nested if...else to find the greatest number
if (num1 > num2) {
if (num1 > num3) {
printf("%d is the greatest number.\n", num1);
} else {
printf("%d is the greatest number.\n", num3);
}
} else {
if (num2 > num3) {
printf("%d is the greatest number.\n", num2);
} else {
printf("%d is the greatest number.\n", num3);
}
}
return 0;
}

Output

Experiment No. 9

Write a program in C to show the grade of a student using else if ladder.


Source Code

#include <stdio.h>
int main()
{
int marks;
// Asking the user to enter the marks
printf("Enter the marks of the student : ");
scanf("%d", &marks);
// Using the else-if ladder to determine the grade
if (marks >= 90 && marks <= 100) {
printf("Grade : A\n");
}
else if (marks >= 80 && marks < 90) {
printf("Grade : B\n");
}
else if (marks >= 70 && marks < 80) {
printf("Grade : C\n");
}
else if (marks >= 60 && marks < 70) {
printf("Grade : D\n");
}
else if (marks >= 50 && marks < 60) {
printf("Grade : E\n");
}
else if (marks >= 0 && marks < 50) {
printf("Grade : F\n");
}
else {
printf("Invalid marks entered. Please enter a number between 0 and 100.\n");
}
return 0;
}

Output

Conclusion

These programs teach us how to use conditional statements in C to solve a variety of problems.
From determining eligibility for voting to checking divisibility and finding the largest number,
these examples show how decision-making can be applied to real-world situations using if-else
and else-if structures. By practicing these types of problems, we can develop a deeper
understanding of how control structures work in programming and how to design logical
algorithms.

What We Have Learned:

1. Control Flow with Conditional Statements: We learned how to use if-else and else-if
statements to make decisions in C programs. These statements allow a program to
execute different blocks of code depending on the condition being true or false.
2. Basic Arithmetic and Logical Operations: By using operators like modulus (%), logical AND
(&&), and arithmetic operators (+, -, etc.), we learned how to perform calculations and
logical checks within the conditions.
3. Handling Edge Cases: We understood how to avoid common programming errors, such
as division by zero and comparing floating-point numbers. For example, we explicitly
checked for c - d != 0 before performing division to avoid errors.
Mistakes to Avoid:

1. Forget to Handle Division by Zero: Always ensure you check for zero before performing
division, as dividing by zero results in undefined behavior.
2. Incorrect Use of Parentheses: When working with logical conditions, be careful about the
order of operations and use parentheses when necessary to ensure the correct
evaluation of conditions.
3. Off-by-One Errors in Conditional Ranges: When creating ranges (like grades or
divisibility), ensure the boundary conditions are correct. For example, 89 might be
intended for grade B, but if you incorrectly set it as C, it can cause an issue.
4. Using Single = Instead of ==: The single equal sign (=) is used for assignment, while the
double equal sign (==) is used for comparison. Always double-check your equality checks
in conditions.

By practicing and avoiding common mistakes, we can create efficient and error-free programs
that handle a variety of real-world tasks. These concepts form the foundation of more advanced
programming topics, and mastering them will make future coding challenges much easier to
tackle.

The End

You might also like