NBC 103 • Practical #2

Largest of Three Numbers & Leap Year Checker

Aim: To write a C program to find the largest of three numbers using nested if-else and verify whether a year is a Leap Year.

Step-by-Step Algorithm & Logic

  • 11. Start and declare integer variables a, b, c, year.
  • 22. Use logical operators (&&) to compare: if (a >= b && a >= c) -> a is largest.
  • 33. Else if (b >= a && b >= c) -> b is largest, else c is largest.
  • 44. For Leap year check: if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) -> Leap Year.
  • 55. Print outputs and terminate.

Complete Tested Source Code

#include <stdio.h>

int main(void) {
    int a = 45, b = 89, c = 32;
    int year = 2024;

    printf("=== Largest of Three Numbers ===\n");
    printf("Numbers: a = %d, b = %d, c = %d\n", a, b, c);
    if (a >= b && a >= c) {
        printf("Result: %d is the largest.\n\n", a);
    } else if (b >= a && b >= c) {
        printf("Result: %d is the largest.\n\n", b);
    } else {
        printf("Result: %d is the largest.\n\n", c);
    }

    printf("=== Leap Year Checker ===\n");
    printf("Testing Year: %d\n", year);
    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
        printf("Result: %d is a LEAP YEAR (366 days).\n", year);
    } else {
        printf("Result: %d is a COMMON YEAR (365 days).\n", year);
    }

    return 0;
}

Sample Input:

a = 45, b = 89, c = 32, year = 2024

Sample Output:

Result: 89 is the largest.
Result: 2024 is a LEAP YEAR (366 days).

Practical Exam Viva Questions (Exp #2)

1 Questions
1

Why is the year 1900 not a leap year even though it is divisible by 4?