Write a C++ Program to Find Largest Number Among Three Numbers

To find the largest number among three numbers in C++, we need to compare each number with the other two numbers and find the maximum value.

We can use the conditional statement ‘if-else’ to compare the numbers and store the largest number in a variable.

Here’s a C++ program to find the largest number among three numbers:

#include <iostream>
using namespace std;

int main() {
   int a, b, c;
   cout << "Enter three numbers: ";
   cin >> a >> b >> c;
   if (a >= b && a >= c) {
      cout << a << " is the largest number." << endl;
   }
   else if (b >= a && b >= c) {
      cout << b << " is the largest number." << endl;
   }
   else {
      cout << c << " is the largest number." << endl;
   }
   return 0;
}

First, we declare three integer variables a, b, and c. We then prompt the user to input the values of the three variables. We use the ‘cin’ statement to read the values entered by the user.

Next, we use the ‘if-else’ statement to compare the values of the variables a, b, and c. We check if the value of ‘a’ is greater than or equal to ‘b’ and ‘c’.

If the condition is true, we print the value of ‘a’ as the largest number.

If the condition is false, we check if the value of ‘b’ is greater than or equal to ‘a’ and ‘c’. If the condition is true, we print the value of ‘b’ as the largest number.

If both the conditions are false, we know that the value of ‘c’ is greater than both ‘a’ and ‘b’. Thus, we print the value of ‘c’ as the largest number.

Finally, we return the value of ‘0’ to end the program.

In conclusion, the program prompts the user to input three numbers, compares them, and outputs the largest number.

This is achieved through the use of conditional statements to check for the largest number among the three numbers.