C Program to Find Largest among Three Numbers

1. Introduction

Finding the largest among three numbers is a common exercise to understand conditional statements in programming. In this article, we'll walk through a C program that identifies the largest number among the three entered by the user.

2. Program Overview

Our program will:

1. Request the user to input three numbers.

2. Compare these numbers to find the largest.

3. Display the largest number to the user.

3. Code Program

#include <stdio.h>  // Incorporate the Standard I/O library for input and output functions

int main() {  // Starting point of our program

    double n1, n2, n3;  // Declare three variables of type double to accommodate potential decimals

    // Asking the user for three numbers
    printf("Enter three different numbers: ");
    scanf("%lf %lf %lf", &n1, &n2, &n3);  // Receive the three numbers

    if (n1 >= n2 && n1 >= n3)  // Check if n1 is the largest
        printf("%.2lf is the largest number.", n1);

    else if (n2 >= n1 && n2 >= n3)  // Check if n2 is the largest
        printf("%.2lf is the largest number.", n2);

    else  // If n1 and n2 aren't the largest, n3 must be
        printf("%.2lf is the largest number.", n3);

    return 0;  // End the program gracefully


Enter three different numbers: 1.23 2.34 3.45
3.45 is the largest number.

4. Step By Step Explanation

1. #include <stdio.h>: By including this header, we're equipped with standard input and output functions.

2. int main(): This denotes the beginning of our program.

3. Declaring variables: We utilize the double data type so that our program can handle numbers with decimals. This makes the program versatile.

4. User input: We ask the user for three distinct numbers, and then capture this input.

5. The comparison:

- Using conditional statements (if-else), the program checks which number is the largest.

- Initially, it checks if n1 is larger or equal to both n2 and n3. If true, n1 is the largest.

- If the first condition fails, it verifies if n2 is the largest.- If neither n1 nor n2 is the largest, then by default, n3 is.

6. Output: The program displays the largest number among the three.

This program offers a straightforward method to compare three numbers, illustrating the power and efficiency of conditional statements in decision-making processes.