Course
Introduction
C++ "Hello, World!" ProgramPrint Number Entered by UserAdd Two NumbersFind Quotient and RemainderFind Size of int, float, double and char in Your SystemSwap Two NumbersFind ASCII Value of a CharacterMultiply two NumbersDecisions and Loops
Check Whether Number is Even or OddCheck Whether a character is Vowel or ConsonantFind Largest Number Among Three NumbersFind All Roots of a Quadratic EquationCalculate Sum of Natural NumbersCheck Leap YearFind FactorialGenerate Multiplication TableDisplay Fibonacci SeriesFind GCDFind LCMReverse a NumberCalculate Power of a NumberCheck Whether a Number is Palindrome or NotCheck Whether a Number is Prime or NotDisplay Prime Numbers Between Two IntervalsCheck Armstrong NumberDisplay Armstrong Number Between Two IntervalsDisplay Factors of a NumberCreate Pyramid and PatternMake a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...caseFunctions
Display Prime Numbers Between Two Intervals Using FunctionsCheck Prime Number By Creating a FunctionCheck Whether a Number can be Express as Sum of Two Prime NumbersFind Sum of Natural Numbers using RecursionCalculate Factorial of a Number Using RecursionFind G.C.D Using RecursionConvert Binary Number to Decimal and vice-versaConvert Octal Number to Decimal and vice-versaConvert Binary Number to Octal and vice-versaReverse a Sentence Using RecursionCalculate Power Using RecursionArrays and Strings
Calculate Average of Numbers Using ArraysFind Largest Element of an ArrayCalculate Standard DeviationAdd Two Matrix Using Multi-dimensional ArraysMultiply Two Matrix Using Multi-dimensional ArraysFind Transpose of a MatrixMultiply two Matrices by Passing Matrix to FunctionAccess Elements of an Array Using PointerSwap Numbers in Cyclic Order Using Call by ReferenceFind the Frequency of Characters in a StringFind the Number of Vowels, Consonants, Digits and White Spaces in a StringRemove all Characters in a String Except AlphabetsFind the Length of a StringConcatenate Two StringsCopy StringsSort Elements in Lexicographical Order (Dictionary Order)C++ Program to Find Largest Number Among Three Numbers
To understand this example, you should have the knowledge of the following C++ programming topics:
In this program, the user is asked to enter three numbers.
Then this program finds out the largest number among three numbers entered by user and displays it with a proper message.
This program can be written in more than one way.
Example 1: Find Largest Number Using if…else Statement
#include <iostream>
using namespace std;
int main() {
double n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
// check if n1 is the largest number
if(n1 >= n2 && n1 >= n3)
cout << "Largest number: " << n1;
// check if n2 is the largest number
else if(n2 >= n1 && n2 >= n3)
cout << "Largest number: " << n2;
// if neither n1 nor n2 are the largest, n3 is the largest
else
cout << "Largest number: " << n3;
return 0;
}
Output
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3
Example 2: Find the Largest Number Using Nested if…else statement
#include <iostream>
using namespace std;
int main() {
double n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
// check if n1 is greater than n2
if (n1 >= n2) {
// if n1 is also greater than n3,
// then n1 is the largest number
if (n1 >= n3)
cout << "Largest number: " << n1;
// but if n1 is less than n3
// but n1 is greater than n2
// then n3 is the largest number
else
cout << "Largest number: " << n3;
}
// else, n2 is greater than n1
else {
// if n2 is also greater than n3,
// then n2 is the largest number
if (n2 >= n3)
cout << "Largest number: " << n2;
// but if n2 is less than n3
// but n2 is greater than n1
// then n3 is the largest number
else
cout << "Largest number: " << n3;
}
return 0;
}
Output
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3
Also Read: