0% found this document useful (0 votes)
9 views3 pages

Lab 3

This document outlines a lab focused on implementing if-else statements in C programming, emphasizing their role in conditional logic. It includes examples such as checking even/odd numbers, determining eligibility to vote, and finding the largest of two numbers. The lab tasks require students to create programs for grade classification, leap year checking, temperature classification, and triangle type checking, along with post lab modifications to enhance these programs.

Uploaded by

zainabnovelsonly
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)
9 views3 pages

Lab 3

This document outlines a lab focused on implementing if-else statements in C programming, emphasizing their role in conditional logic. It includes examples such as checking even/odd numbers, determining eligibility to vote, and finding the largest of two numbers. The lab tasks require students to create programs for grade classification, leap year checking, temperature classification, and triangle type checking, along with post lab modifications to enhance these programs.

Uploaded by

zainabnovelsonly
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/ 3

Programming Fundamentals

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.

Examples Highlighting the Use of Syntax

Example 1: Checking Even or Odd Number


#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

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);

if (num1 > num2) {


printf("%d is larger.\n", num1);
} else if (num2 > num1) {
printf("%d is larger.\n", num2);
} else {
printf("Both numbers are equal.\n");
}
return 0;
}
Lab Tasks

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:

o Marks >= 90: Grade A

o Marks >= 80: Grade B

o Marks >= 70: Grade C

o Marks >= 60: Grade D

o Marks < 60: Grade F

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.

3. Temperature Classification: Develop a program that takes the temperature as input


and classifies it as:

o Above 30°C: Hot

o Between 15°C and 30°C: Moderate

o Below 15°C: Cold

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.

Post Lab Tasks


1. Modify the voting eligibility program to also check if a person is eligible to run for
president (age >= 35).

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).

You might also like