In this program, we are going to find the largest number among three numbers, similar to the previous one, but it is nested if-else version.
Logic
Let three variables be: A = 400, B = 200 and C = 300
The logic goes like this:
- if A >= B then check for if A >= C, then print A else print C.
- else part: if B >= C then print B else print C.
See the below-shown flow diagram to better understand.
Program
#include <iostream> using namespace std; int main() { float num1, num2, num3; cout << "Enter 1st number : "; cin >> num1; cout << endl << "Enter 2nd number : "; cin >> num2; cout << endl << "Enter 3rd number : "; cin >> num3; if(num1 >= num2) { if(num1 >= num3) { cout << endl << num1 << " is largest number"; } else { cout << endl << num3 << " is largest number"; } } else { if(num2 >= num3) { cout << endl << num2 << " is largest number"; } else { cout << endl << num3 << " is largest number"; } } return 0; }
Output
Enter 1st number : 100
Enter 2nd number : 10
Enter 3rd number : 99
100 is largest number