C Programming • C Basics, Operators, Loops
C Programming / nested if — Exercises

nested if — Exercises

Practical 2 C Basics, Operators, Loops

Write a C program to check whether a number is positive and, if it is positive, further check whether it is even or odd.

Practical / Solution

Nested if — Exercise 1

Problem

Write a C program to check whether a number is positive and, if it is positive, further check whether it is even or odd.

Program

#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); // First check whether the number is positive if (num > 0) { // Further check whether the positive number is even or odd if (num % 2 == 0) printf("The number is positive and even.\n"); else printf("The number is positive and odd.\n"); } return 0; }

Explanation

The inner if statement is executed only when the outer if condition is true. This is called nested decision-making.

Expected Output

Enter a number: 24

The number is positive and even.

Nested if — Exercise 2

Problem

Write a C program to check whether a person is eligible for a driving test. If the age is 18 or more, further check whether the person has a valid learner licence.

Program

#include <stdio.h> int main() { int age, licence; printf("Enter age: "); scanf("%d", &age); // First check the minimum age if (age >= 18) { printf("Do you have a learner licence? (1 = Yes, 0 = No): "); scanf("%d", &licence); // Check licence only for eligible age if (licence == 1) printf("Eligible for the driving test.\n"); } return 0; }

Explanation

The second condition is checked only after the first condition confirms that the age requirement is satisfied.

Expected Output

Enter age: 20

Do you have a learner licence? (1 = Yes, 0 = No): 1

Eligible for the driving test.

Nested if — Exercise 3

Problem

Write a C program to determine whether a student is eligible for an examination. The student must have attendance of at least 75%. If eligible, further check whether the student has paid the examination fee.

Program

#include <stdio.h> int main() { float attendance; int feePaid; printf("Enter attendance percentage: "); scanf("%f", &attendance); // First check attendance if (attendance >= 75) { printf("Has examination fee been paid? (1 = Yes, 0 = No): "); scanf("%d", &feePaid); // Check fee payment if (feePaid == 1) printf("Student is eligible for the examination.\n"); } return 0; }

Explanation

The inner condition is dependent on the outer attendance condition. Therefore, the program uses nested if statements.

Expected Output

Enter attendance percentage: 82

Has examination fee been paid? (1 = Yes, 0 = No): 1

Student is eligible for the examination.

Nested if — Exercise 4

Problem

Write a C program to find the largest of three numbers using nested if statements.

Program

#include <stdio.h> int main() { int a, b, c, largest; printf("Enter three numbers: "); scanf("%d %d %d", &a, &b, &c); // Compare the first two numbers if (a > b) { // Compare a with c if (a > c) largest = a; else largest = c; } else { // Compare b with c if (b > c) largest = b; else largest = c; } printf("Largest = %d\n", largest); return 0; }

Explanation

The first if compares a and b. A second condition then compares the larger candidate with c.

Expected Output

Enter three numbers: 25 68 42

Largest = 68

Nested if — Exercise 5

Problem

Write a C program to check whether a number is a two-digit number and, if it is, determine whether it is even or odd.

Program

#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); // Check whether the number is two-digit if ((num >= 10 && num <= 99) || (num <= -10 && num >= -99)) { // Determine whether the two-digit number is even or odd if (num % 2 == 0) printf("The two-digit number is even.\n"); else printf("The two-digit number is odd.\n"); } return 0; }

Explanation

The outer condition first checks the number of digits. Only when the number is two-digit does the inner condition check even or odd.

Expected Output

Enter a number: 48

The two-digit number is even.

Nested if — Exercise 6

Problem

Write a C program to determine a student's result using nested if. First check whether the student has passed in the subject. If passed, check whether the marks are 75 or more for distinction.

Program

#include <stdio.h> int main() { int marks; printf("Enter marks: "); scanf("%d", &marks); // First check whether the student has passed if (marks >= 50) { // Check distinction after passing if (marks >= 75) printf("Pass with distinction.\n"); else printf("Pass.\n"); } return 0; }

Explanation

The inner condition is checked only if the minimum passing marks condition is satisfied.

Expected Output

Enter marks: 82

Pass with distinction.

Nested if — Exercise 7

Problem

Write a C program to check whether a number is positive, and if positive, determine whether it is greater than 100 or not.

Program

#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); // First check whether the number is positive if (num > 0) { // Check whether it is greater than 100 if (num > 100) printf("Positive number greater than 100.\n"); else printf("Positive number but not greater than 100.\n"); } return 0; }

Explanation

The second decision is made only when the first condition confirms that the number is positive.

Expected Output

Enter a number: 150

Positive number greater than 100.

Nested if — Exercise 8

Problem

Write a C program to calculate a discount based on purchase amount. First check whether the amount is at least 1000. If yes, further check whether it is at least 5000 for a higher discount.

Program

#include <stdio.h> int main() { float amount; printf("Enter purchase amount: "); scanf("%f", &amount); // Check minimum amount for discount if (amount >= 1000) { // Check higher discount category if (amount >= 5000) printf("10%% discount applicable.\n"); else printf("5%% discount applicable.\n"); } return 0; }

Explanation

The outer condition identifies customers eligible for a discount. The inner condition separates them into two discount categories.

Expected Output

Enter purchase amount: 6500

10% discount applicable.

Nested if — Exercise 9

Problem

Write a C program to check whether a year is valid for leap-year processing. If it is divisible by 4, further check the special conditions involving 100 and 400.

Program

#include <stdio.h> int main() { int year; printf("Enter year: "); scanf("%d", &year); // First check divisibility by 4 if (year % 4 == 0) { // Handle century years separately if (year % 100 == 0) { if (year % 400 == 0) printf("Leap year.\n"); else printf("Not a leap year.\n"); } else { printf("Leap year.\n"); } } return 0; }

Explanation

Nested conditions are used to handle the special case of century years. A year divisible by 100 needs an additional check for divisibility by 400.

Expected Output

Enter year: 2000

Leap year.

Nested if — Exercise 10

Problem

Write a C program to find the largest among three numbers and also check whether the largest number is positive or negative.

Program

#include <stdio.h> int main() { int a, b, c, largest; printf("Enter three numbers: "); scanf("%d %d %d", &a, &b, &c); // Find the largest number if (a > b) { if (a > c) largest = a; else largest = c; } else { if (b > c) largest = b; else largest = c; } printf("Largest = %d\n", largest); // Check the sign of the largest number if (largest >= 0) { if (largest == 0) printf("The largest number is zero.\n"); else printf("The largest number is positive.\n"); } else { printf("The largest number is negative.\n"); } return 0; }

Explanation

This program demonstrates multiple levels of nested decision-making. It first finds the largest value and then applies another decision process to determine its sign.

Expected Output

Enter three numbers: -20 -5 -15

Largest = -5

The largest number is negative.