Write a program to find greatest from three numbers

Write a program to find greatest from three numbers

 

Assignment 4

Write a
program to find greatest from three numbers.

We can
solve this problem using two ways, explain as below-

Option 1: Using if statement.

Program
Code with explanation:

1.   
#include
<iostream>

2.   
using
namespace std;

3.   
int
main()

4.   
{

5.   
      int
a1,a2,a3;

6.   
      cout<<“Accept
three integer values to find greatest from three numbers “;

7.   
      cout<<“\nAccept
First Number “;

8.   
      cin>>a1;

9.   
      cout<<“\nAccept
Second Number “;

10.       cin>>a2;

11.       cout<<“\nAccept
third Number “;

12.       cin>>a3;

13.       cout<<“Display
Greatest from three numbers \n”;

14.       //
find greater Number.

15.       if((a1>a2)&(a1>a3))

16.                   cout<<“\n”<<a1<<”
is greater than “<<a2<<” and “<<a3;

17.       if((a2>a1)&(a2>a3))

18.                   cout<<“\n”<<a2<<”
is greater than “<<a1<<” and “<<a3;

19.       if((a3>a1)&(a3>a2))

20.                   cout<<“\n”<<a3<<”
is greater than “<<a1<<“and “<<a2;

21.       return
0;

22. }

Explanation
of Code:

Line
No. 1 and 2: include the Library file and stander input – output functions.

Line
No. 3: main function header.

Line
No. 4 and 22: Begin and end of main function respectively.

Line
No. 5: Declare the three integer variables.

Line
No. 6 to 12: Accept three integer numbers with appropriate massage. 

Line
No. 13 and 14: Display massages “Display Greatest from three numbers”.

Line No. 15: To check first
integer number greater than other than two if it’s true then goes to Line No 16
and print greater number massage.

Line No. 17: To check second
integer number greater than other than two if it’s true then goes to Line No 18
and print greater number massage.

Line No. 19: To check third
integer number greater than other than two if it’s true then goes to Line No 20
and print greater number message.

Line No. 21: return statement
appropriate with main function return data type.

Program
Code for run:

#include
<iostream>

using
namespace std;

int
main()

{

            int
a1,a2,a3;

            cout<<“Accept
three integer values to find greatest from three numbers “;

            cout<<“\nAccept
First Number “;

            cin>>a1;

            cout<<“\nAccept
Second Number “;

            cin>>a2;

            cout<<“\nAccept
third Number “;

            cin>>a3;

            cout<<“Display
Greatest from three numbers \n”;

            //
find greater Number.

            if((a1>a2)&(a1>a3))

                        cout<<“\n”<<a1<<”
is greater than “<<a2<<” and “<<a3;

            if((a2>a1)&(a2>a3))

                        cout<<“\n”<<a2<<”
is greater than “<<a1<<” and “<<a3;

            if((a3>a1)&(a3>a2))

                        cout<<“\n”<<a3<<”
is greater than “<<a1<<“and “<<a2;

    return 0;

}

Output
of Program:

Accept
three integer values to find greatest from three numbers

Accept
First Number 500

Accept
Second Number 800

Accept
third Number 400

Display
Greatest from three numbers

800
is greater than 500 and 400

Option 2: Using Ternary Operator

Program
Code for run:

#include <iostream>

using namespace std;

int main()

{

   
int a1,a2,a3,MAX;

   
cout<<“Accept three integer values to find greatest from
three numbers “;

   
cout<<“\nAccept First Number “;

  
 cin>>a1;

   
cout<<“\nAccept Second Number “;

   
cin>>a2;

   
cout<<“\nAccept third Number “;

   
cin>>a3;

   
cout<<“Display Greatest from three numbers \n”;

   
MAX=(a1>a2)&&(a1>a3)?a1:(a2>a3)?a2:a3;

   
cout<<“Greatest from three numbers = “<<MAX;

   
return 0;


Output
of Program:

Accept three integer values to find
greatest from three numbers

Accept First Number 222

Accept Second Number 888

Accept third Number 666

Display Greatest from three numbers

Greatest from three numbers = 888

            In
this program we use Ternary Operator to find greatest
from three numbers.

Source link

Leave a Comment

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


Scroll to Top