In this post, you’ll learn how to find the Largest Number Among Three Numbers using C++ Programming Language.
This lesson, you will learn how to find the Largest Number Among Three Numbers, with a simple comparison operator and decision making statements using the C++ language. Let’s look at the below source code.
How to Find the Largest Number Among Three Numbers?
RUN CODE SNIPPETSource Code
#include <iostream> using namespace std; int main() { int a,b,c; cin>> a >> b >> c ; cout<< "Enter three numbers: "<< a << b << c <<endl; if(a>b) { if(a>c) cout<<"\n "<< a <<" is largest number"; else cout<<"\n "<< c <<" is largest number"; } else { if(b>c) cout<<"\n "<< b <<" is largest number"; else cout<<"\n "<< c <<" is largest number"; } return 0; }
Input
1 2 3
Output
Enter three numbers: 123 3 is largest number
#include <iostream>
- This line which is called the header file.Â
#include
statement tells the compiler to use available files and<iostream>
is the name of the specific that we have used in this code. The<iostream>
file stands for Input and Output statement.
using namespace std;
- The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.
int main();
- This line usually controls the function of the code, as it calls the functions to perform their tasks.
- The
int main()
shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.
{ }
- The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
- Every statement and value between these braces belong to the main function.
The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.
- To start, we first declare three variables (a, b, c) as integers, get them from the user using
cin >>
andcout <<
to display them. - Next we compare each of the integers, using the comparison operator ‘>’ and display the respective output statements using the decision making statements
if else
. - In the first comparison statement
(a>b)
 , if the condition is satisfied, the nested if else statement is executed, where there is another comparison statement(a>c)
, based on whether the condition is true or false the output statements are displayed. - If the first comparison statement is false, the function moves to the else statements,
(b>c)
based on whether this condition is true or false, the output statements are displayed. - End the function with
return 0;
this returns the function to main( ).
Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.
Additional Source Code
You can also try the source code given below, the following code is structured to find the Largest Number Among Three Numbers, using comparison operator and logical AND operator (&&).
RUN CODE SNIPPET#include <iostream> using namespace std; int main() { float a, b, c; cin >> a >> b >> c; cout << "Enter three numbers: "<< a << ", " << b << ", " << c <<endl; if(a > b && a > c) cout << "\n Largest number: " << a; if(b > a && b > c) cout << "\n Largest number: " << b; if(c > a && c > b) cout << "\n Largest number: " << c; return 0; }
Input
1.4 2.12 3.98
Output
Enter three numbers: 1.4, 2.12, 3.98 Largest number: 3.98
- In this source code we declare the variables (a, b, c) as float values, that is decimal numbers.
- Using if statements we declare the conditions with the comparison operator and the logical operator
(a > b && a > c)
– This statement compares a with b and c. Only if both the comparison statements are true the condition is satisfied, this is the function of the logical Operator &&, when the condition is satisfied the output is displayed. If any one of the comparison statements is not true, the function moves to the next Condition.- This is another simple source code to find the Largest Number Among Three Numbers.