Lab 3
Lab 3
Lab 3
Conditional Logic
Implementing if-else Statements
Introduction
Conditional statements are fundamental in programming, allowing a program to make decisions
based on different conditions. The if-else statement in C enables the execution of specific blocks
of code based on whether a condition evaluates to true or false. This lab will help students
understand the syntax, structure, and practical applications of if-else statements in C
programming.
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
Example 2: Checking Positive, Negative, or Zero
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("The number is positive.\n");
} else if (num < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}
Example 3: Determining Eligibility to Vote
#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;
}
Example 4: Finding the Largest of Two Numbers
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
1. Grade Classification: Write a C program that takes a student’s marks as input and
prints the corresponding grade based on the following conditions:
2. Leap Year Checker: Write a program to check whether a given year is a leap year or not.
o A year is a leap year if it is divisible by 4 but not by 100, except if it is also divisible
by 400.
4. Triangle Type Checker: Write a program that takes three sides of a triangle as input and
determines whether it is an equilateral, isosceles, or scalene triangle.
2. Extend the largest of two numbers program to find the largest among three numbers.
3. Enhance the grade classification program to display a message if the marks entered
are out of range (less than 0 or greater than 100).