C++ program to find the largest number among three number using if else

C++ program to find the largest number among three number using if else statement:

In this article, you’ll learn to find the largest number of the three number in details with example and explanation in the C++ language without giving input from the keyboard or giving input from the keyboard.

Q: What is the purpose of this program

Ans: We create a program in C++ language that helps us to find the largest among three number without giving input from the keyboard or giving input from the keyboard.

There are many processes to find the largest among three number some are below given:

  • Using if statement
  • Using if…else statement
  • Using Nested if…else statement

Here, you find the largest number among three number by using if statement.

Explanation:

  • First, create three variable here we create num1, num2, num3 for storing three number then
  • Check num1 with num2 and num1 with num3
  • If num1 is greater than num2 and num3, then print on the screen the value of num1 and check the next statement.
  • Check num2 with num3 and num2 with num1
  • If num2 is greater than num3 and num1, then print on the screen the value of num2 and check the next statement.
  • Check num3 with num1 and num3 with num2
  • If num3 is greater num1 and num2, then print on the screen the value of num3 and stop checking and print on the screen which is largest among three number.

Q: Write a program in C++ to find largest among three number using if…esle

Solution:

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

int num1=41, num2=44, num3=46;

if(num1>=num2 && num1>=num3)

{

cout<<“Largest number is ” <<num1;

}

if(num2>=num3 && num2>=num1)

{

cout<< “Largest number is ” <<num2;

}

if(num3>=num1 && num3>=num2)

{

cout<<“Largest number is “<<num3;

}

return 0;

 

}

 

The output of this program:

Largest number is 46

 

Q: Write a program in C++ to find largest among three number using if…esle, input is given by user

Solution:

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

int num1, num2, num3;

cout<<“Enter three number: “;

cin>> num1>>num2>>num3;

if(num1>=num2 && num1>=num3)

{

cout<<“Largest number is ” <<num1;

}

if(num2>=num3 && num2>=num1)

{

cout<< “Largest number is ” <<num2;

}

if(num3>=num1 && num3>=num2)

{

cout<<“Largest number is “<<num3;

}

return 0;

 

}

 

The output of this program:

Enter three number: 45

65

52

Largest number is 65

Note: In the above two program supports only integer value if enter a real number like 2.2, 4.5, 8.6 then it will not show the result. To show the result we declare the variable with float in place of int. Example are given below:

Q: Write a program in C++ to find largest among three number using if…esle

Solution:

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

float num1=41.258, num2=41.654,num3=41.569;

if(num1>=num2 && num1>=num3)

{

cout<<“Largest number is ” <<num1;

}

if(num2>=num3 && num2>=num1)

{

cout<< “Largest number is ” <<num2;

}

if(num3>=num1 && num3>=num2)

{

cout<<“Largest number is “<<num3;

}

return 0;

 

}

 

The Output of this program:

Largest number is 41.654

 

Q: Write a program in C++ to find largest among three number using if…esle, input is given by user

Solution:

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

float num1, num2, num3;

cout<<“Enter three number: “;

cin>> num1>>num2>>num3;

if(num1>=num2 && num1>=num3)

{

cout<<“Largest number is ” <<num1;

}

if(num2>=num3 && num2>=num1)

{

cout<< “Largest number is ” <<num2;

}

if(num3>=num1 && num3>=num2)

{

cout<<“Largest number is “<<num3;

}

return 0;

 

}

 

The output of this program:

Enter three number: 89.231

88.235

85.528

Largest number is 89.231

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

itetrade.com