C Programming • C Basics, Operators, Loops
C Programming / if-else — Exercises

if-else — Exercises

Practical 2 C Basics, Operators, Loops

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

Practical / Solution

if-else — Exercise 1

Problem

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

Program

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

Explanation

The if block executes when the condition is true. Otherwise, the else block executes.

Expected Output

Enter a number: -12

The number is negative.

if-else — Exercise 2

Problem

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

Program

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

Explanation

If the remainder after division by 2 is zero, the number is even; otherwise, it is odd.

Expected Output

Enter a number: 17

The number is odd.

if-else — Exercise 3

Problem

Write a C program to check whether a student has passed or failed. A student passes if marks are 50 or more.

Program

#include <stdio.h> int main() { int marks; printf("Enter marks: "); scanf("%d", &marks); // Check passing condition if (marks >= 50) printf("Pass.\n"); else printf("Fail.\n"); return 0; }

Explanation

The program compares the marks with 50. If the condition is true, the student passes; otherwise, the student fails.

Expected Output

Enter marks: 42

Fail.

if-else — Exercise 4

Problem

Write a C program to find the greater of two numbers using if-else.

Program

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

Explanation

The two numbers are compared. If a is greater than b, a is displayed; otherwise, b is displayed.

Expected Output

Enter two numbers: 25 40

40 is greater or equal.

if-else — Exercise 5

Problem

Write a C program to check whether a person is eligible to vote. A person is eligible if the age is 18 or more.

Program

#include <stdio.h> int main() { int age; printf("Enter age: "); scanf("%d", &age); // Check voting eligibility if (age >= 18) printf("Eligible to vote.\n"); else printf("Not eligible to vote.\n"); return 0; }

Explanation

The condition checks whether the age is at least 18. Both possible outcomes are handled by if-else.

Expected Output

Enter age: 16

Not eligible to vote.

if-else — Exercise 6

Problem

Write a C program to check whether a number is divisible by both 5 and 11.

Program

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

Explanation

The logical AND operator && requires both divisibility conditions to be true.

Expected Output

Enter a number: 55

The number is divisible by both 5 and 11.

if-else — Exercise 7

Problem

Write a C program to determine whether a character is a vowel or a consonant.

Program

#include <stdio.h> int main() { char ch; printf("Enter a character: "); scanf(" %c", &ch); // Check whether the character is a vowel if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') printf("The character is a vowel.\n"); else printf("The character is a consonant.\n"); return 0; }

Explanation

The character is compared with all five vowels in both lowercase and uppercase forms.

Expected Output

Enter a character: E

The character is a vowel.

if-else — Exercise 8

Problem

Write a C program to check whether a year is a leap year.

Program

#include <stdio.h> int main() { int year; printf("Enter year: "); scanf("%d", &year); // Check leap year condition if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) printf("Leap year.\n"); else printf("Not a leap year.\n"); return 0; }

Explanation

A year is considered a leap year when it is divisible by 400, or divisible by 4 but not by 100.

Expected Output

Enter year: 2024

Leap year.

if-else — Exercise 9

Problem

Write a C program to check whether a person receives a discount. A discount is given when the purchase amount is 5000 or more.

Program

#include <stdio.h> int main() { float amount; printf("Enter purchase amount: "); scanf("%f", &amount); // Check discount eligibility if (amount >= 5000) printf("Discount is applicable.\n"); else printf("Discount is not applicable.\n"); return 0; }

Explanation

The purchase amount is compared with the minimum amount required for the discount.

Expected Output

Enter purchase amount: 6500

Discount is applicable.

if-else — Exercise 10

Problem

Write a C program to check whether a number lies within the range 1 to 100.

Program

#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); // Check whether the number lies in the range if (num >= 1 && num <= 100) printf("The number is within the range.\n"); else printf("The number is outside the range.\n"); return 0; }

Explanation

The logical AND operator ensures that both conditions are true: the number must be at least 1 and at most 100.

Expected Output

Enter a number: 75

The number is within the range.