C++ | Smallest number

Introduction

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

Program to find smallest 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 smallest";
else
cout<<b<<" is smallest";
//get character
getch();
}

Output

Enter the no.
23
Enter the no.
56
23 is smallest

Program to find smallest 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 smallest";
else
{
if(b<a && b<c)
cout<<b<<" is smallest";
else
cout<<c<<"is smallest";
}
//get character
getch();
}

Output

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

Program to find smallest 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 smallest";
else
{
if(b<a && b<c && b<d)
cout<<b<<" is smallest";
else
{
if(c<a && c<b && c<d)
cout<<c<<" is smallest";
else
cout<<d<<"is smallest";
}
}
//get character
getch();
}

Output

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

Program to find smallest 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 smallest";
else
cout<<b<<" is smallest";
//get character
getch();
}

Output

Enter the no.
23.5
Enter the no.
23.3
23.3 is smallest

How does it work

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

Extending it

The program can be extended by using more variables to find smallest 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 smallest number.

At the end

You learnt creating the c++ program of finding a smallest 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