Note: This program may misbehave if you change the data type of number, For running and executing this program all data types should be the same. Ex float, long and double.
C++ Program to Find Largest of Three Numbers Using IF-ELSE
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "Enter The 3 Numbers :\n\n";
cin >> a >> b >> c;
if (a > b && a > c)
{
cout << "First Number is Largest \n";
}
else if (b > a && b > c)
{
cout << "Second Number is Largest \n";
}
else if (c > a && c > b)
{
cout << "Third Number is Largest \n";
}
else
cout << "All Numbers Are Equal \n";
return 0;
}
Find the Largest of Three Numbers
Step 1
So for this problem, we are taking three numbers 10, 20, and 30. If the first number is greater than the remaining two then print the first one in greater, so according to steps 1 - 10 is not greater than 20 and 30 so the first step is not working now follow step 2.
Step 2
If the Second number is greater than the remaining two then print the Second one in greater, so according to step 2 - 20 is greater than 10 but not greater than 30 so the step fails now follow step 3.
Step 3
If the Third number is greater than the remaining two then print the Third one in greater, so according to steps 1 and step 2 - 30 is greater than 20 and 10 here step 3 working now no need to perform step 4, we get our answer and program will print "Third Number is Greatest".
Step 4
If step 1 to step 3 is not true then all three numbers are equal. This is a special case if all numbers are equal. For this step, if all three numbers are equal like 10, 10, and 10 then the program will print "All Numbers Are Equal".
The Output of the Largest of Three Numbers in C++
- C++ Program to Count Positive and Negative Numbers Using While Loop
- C++ Program to Convert Celsius to Fahrenheit And Vice Versa
- C++ Program to Display the Grade of a Student Using Switch Case
- C++ Program to Perform Arithmetic Operations Using Switch Case
- C++ Program to Find Area of a Triangle / Square / Circle / Rectangle Using Switch Statement
0 Comments: