C++ Online Compiler
Example: Find Largest Number Among Three (Nested if-else) in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Find Largest Number Among Three (Nested if-else) #include <iostream> using namespace std; int main() { // Step 1: Declare three variables to store the numbers float num1, num2, num3; // Step 2: Prompt the user to enter three numbers cout << "Enter three numbers: "; cin >> num1 >> num2 >> num3; // Step 3: Use nested if-else to find the largest number if (num1 >= num2) { // num1 is greater than or equal to num2 if (num1 >= num3) { // num1 is also greater than or equal to num3 cout << "The largest number is: " << num1 << endl; } else { // num3 is greater than num1 (and num1 >= num2) cout << "The largest number is: " << num3 << endl; } } else { // num2 is greater than num1 if (num2 >= num3) { // num2 is also greater than or equal to num3 cout << "The largest number is: " << num2 << endl; } else { // num3 is greater than num2 (and num2 > num1) cout << "The largest number is: " << num3 << endl; } } return 0; }
Output
Clear
ADVERTISEMENTS