Google News
logo
C++ Program to Find Largest Number Among Three Numbers
In the following example of C++ program that finds the largest number among three numbers entered by the user :
Program :
#include <iostream>

int main() {
    int num1, num2, num3, largest;
    std::cout << "Enter three numbers: ";
    std::cin >> num1 >> num2 >> num3;
    if (num1 >= num2 && num1 >= num3)
        largest = num1;
    else if (num2 >= num1 && num2 >= num3)
        largest = num2;
    else
        largest = num3;
    std::cout << "Largest number among " << num1 << ", " << num2 << ", and " << num3 << " is " << largest;
    return 0;
}
Output :
Enter three numbers: 14
19
11
Largest number among 14, 19, and 11 is 19
* This program declares four variables: num1, num2, num3, and largest, which are all of type int.

* The program then prompts the user to enter three numbers using the std::cout and std::cin objects from the iostream library.

* The >> operator is used to input the user's numbers into the variables num1, num2, and num3.

* The program then uses conditional statements to compare the three numbers and determine the largest one. The largest number is stored in the largest variable.

* Finally, the program uses std::cout to output a message that displays the largest number among the three numbers to the console.