In this Program, you’ll learn to find the Largest Number Among Three numbers using if, if else and nested if else statements.
To understand this program to find the Largest Number Among Three, you should have the knowledge of following C++ programming topics:
- C++ if, if…else and Nested if…else
In this program, a user is asked to enter three numbers.
Then this program finds out the largest number of three numbers entered by the user and displays it with a proper message.
This program can be used in more than one way.
Program to Find Largest Number Using if Statement
#include <iostream> using namespace std; int main() { float n1, n2, n3; cout << "Enter three numbers: "; cin >> n1 >> n2 >> n3; if(n1 >= n2 && n1 >= n3) { cout << "Largest number: " << n1; } if(n2 >= n1 && n2 >= n3) { cout << "Largest number: " << n2; } if(n3 >= n1 && n3 >= n2) { cout << "Largest number: " << n3; } return 0; }
Output
Enter three numbers: 2.3 8.3 -4.2 Largest number: 8.3
Program to Find Largest Number Using if…else Statement
#include <iostream> using namespace std; int main() { float n1, n2, n3; cout << "Enter three numbers: "; cin >> n1 >> n2 >> n3; if((n1 >= n2) && (n1 >= n3)) cout << "Largest number: " << n1; else if ((n2 >= n1) && (n2 >= n3)) cout << "Largest number: " << n2; else cout << "Largest number: " << n3; return 0; }
Output
Enter three numbers: 2.3 8.3 -4.2 Largest number: 8.3
Program to Find Largest Number Using Nested if…else statement
#include <iostream> using namespace std; int main() { float n1, n2, n3; cout << "Enter three numbers: "; cin >> n1 >> n2 >> n3; if (n1 >= n2) { if (n1 >= n3) cout << "Largest number: " << n1; else cout << "Largest number: " << n3; } else { if (n2 >= n3) cout << "Largest number: " << n2; else cout << "Largest number: " << n3; } return 0; }
Output
Enter three numbers: 2.3 8.3 -4.2 Largest number: 8.3
Related Programs
- C++ Program to Check Whether Number is Even or Odd
- C++ Program to Check Whether a character is Vowel or Consonant
- C++ Program to Find All Roots of a Quadratic Equation
- C++ Program to Calculate Sum of Natural Numbers
- C++ Program to Check Leap Year
Ask your questions and clarify your/others doubts on Largest Number Among Three by commenting. Documentation