C++ Program to Find the Largest Among Three Numbers

1. Introduction

In computational tasks, determining the highest or lowest value is a frequent requirement. Whether it's evaluating student grades, comparing stock prices, or even scoring game points, we often need to identify the maximum or minimum value among a set of numbers. In this blog post, we will learn how to write a C++ program that finds the largest value among three distinct numbers.

2. Program Overview:

The algorithm to determine the largest among three numbers is pretty straightforward:

1. Acquire three distinct numbers from the user.

2. Initiate a comparison: Check if the first number is greater than the other two.

3. If it isn't, compare the second number with the first and the third.

4. If the second isn’t the largest either, by default, the third number is the largest.

3. Code Program

#include <iostream>
using namespace std;

int main() {
    double num1, num2, num3;

    // Prompting user for input
    cout << "Enter three distinct numbers: "; // Ask the user to input three distinct numbers
    cin >> num1 >> num2 >> num3; // Read the input and store it in num1, num2, and num3

    // Comparing numbers to find the largest
    if(num1 >= num2 && num1 >= num3) {
        cout << "The largest number is: " << num1 << endl; // Output the largest number
    } else if(num2 >= num1 && num2 >= num3) {
        cout << "The largest number is: " << num2 << endl; // Output the largest number
    } else {
        cout << "The largest number is: " << num3 << endl; // Output the largest number
    }

    return 0; // Indicate successful execution by returning 0
}

Output:

Enter three distinct numbers: 1.5 2.5 3.5
The largest number is: 3.5

4. Step By Step Explanation

1. Headers and Namespace: The program begins by including the necessary headers and declaring the standard namespace, a standard procedure in C++ programs.

#include <iostream>
using namespace std;

2. Variable Declaration: Three double type variables are initialized to store the numbers input by the user.

    double num1, num2, num3;

3. User Input: The program prompts the user for three numbers and stores them in the declared variables.

    // Prompting user for input
    cout << "Enter three distinct numbers: "; // Ask the user to input three distinct numbers
    cin >> num1 >> num2 >> num3; // Read the input and store it in num1, num2, and num3

4. Comparison Logic: Employing the if-else construct, the program evaluates which of the three numbers is the largest. It sequentially checks num1, then num2, and if neither is the largest, it concludes that num3 is the largest.

    // Comparing numbers to find the largest
    if(num1 >= num2 && num1 >= num3) {
        cout << "The largest number is: " << num1 << endl; // Output the largest number
    } else if(num2 >= num1 && num2 >= num3) {
        cout << "The largest number is: " << num2 << endl; // Output the largest number
    } else {
        cout << "The largest number is: " << num3 << endl; // Output the largest number
    }

5. Displaying the Result: Using the cout statement, the program displays the largest number to the user.

cout << "The largest number is: " << num1 << endl; // Output the largest number

6. Program Termination: The program gracefully terminates by returning 0, signaling successful execution.

    return 0; // Indicate successful execution by returning 0


Comments