C++ | Largest number

Introduction

The program will find the largest number of the given numbers. The first one will find largest from two numbers. The next will find the largest from three numbers. The next one will find from four numbers. And the last program will find the largest decimal number for given two numbers. You can extend the program by adding more variables for finding largest among them. Go enjoy the program. Lets begin……………

Program to find largest of two numbers. 

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variables type int
int a,b;
//Input the numbers save them in variables
cout<<"Enter the no."<<endl;
cin>>a;
cout<<"Enter the no."<<endl;
cin>>b;
//condition if-else statement
if(a>b)
cout<<a<<" is largest";
else
cout<<b<<" is largest";
//get character
getch();
}

Output

Enter the no.
23
Enter the no.
56
56 is largest

Program to find largest of three numbers. 

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variables type int
int a,b,c;
//Input the numbers save them in variables
cout<<"Enter the no."<<endl;
cin>>a;
cout<<"Enter the no."<<endl;
cin>>b;
cout<<"Enter the no."<<endl;
cin>>c;
//condition if-else statement
if(a>b && a>c)
cout<<a<<" is largest";
else
{
if(b>a && b>c)
cout<<b<<" is largest";
else
cout<<c<<"is largest";
}
//get character
getch();
}

Output

Enter the no.
23
Enter the no.
56
Enter the no.
67
67 is largest

Program to find largest of four numbers. 

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variables type int
int a,b,c,d;
//Input the numbers save them in variables
cout<<"Enter the no."<<endl;
cin>>a;
cout<<"Enter the no."<<endl;
cin>>b;
cout<<"Enter the no."<<endl;
cin>>c;
cout<<"Enter the no."<<endl;
cin>>d;
//condition if-else statement
if(a>b && a>c && a>d)
cout<<a<<" is largest";
else
{
if(b>a && b>c && b>d)
cout<<b<<" is largest";
else
{
if(c>a && c>b && c>d)
cout<<c<<" is largest";
else
cout<<d<<"is largest";
}
}
//get character
getch();
}

Output

Enter the no.
23
Enter the no.
56
Enter the no.
67
Enter the no.
10
67 is largest

Program to find largest of two decimal numbers. 

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variables type float
float a,b;
//Input the numbers save them in variables
cout<<"Enter the no."<<endl;
cin>>a;
cout<<"Enter the no."<<endl;
cin>>b;
//condition if-else statement
if(a>b)
cout<<a<<" is largest";
else
cout<<b<<" is largest";
//get character
getch();
}

Output

Enter the no.
23.5
Enter the no.
23.3
23.5 is largest

How does it work

  1. You enter the number or alpahbet  in and is saved in ‘a’variable..
  2. Decides the largest number and prints it.

Extending it

The program can be extended by using more variables to find largest number.You can take the inputs and print it accordingly.

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variables as int or float (name them as you want.)
  5. Add cout and cin of variable of inputs.(as shown in program)
  6. Add the conditional statement if-else.
  7. Cout the largest number.

At the end

You learnt creating the c++ program of finding a largest number. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s