This program is similar to the previous one, with a single modification, here we will use if else statement to find the largest number.
Logic
The logic to find the largest number among the three numbers, we have to compare each number with the other two numbers.
Let three variables be: A = 400, B = 200 and C = 300
- if A > B and A > C, then print A.
- else if B > A and B > C, then print B.
- else print C.
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 && num1 >= num3) { cout << endl << num1 << " 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 : 10
Enter 2nd number : 200
Enter 3rd number : 11
200 is largest number