Smallest number among three numbers in C++
#include<iostream>
using namespace std;
int main()
{
int num1,num2,num3;
cout<<"enter first number : ";
cin>>num1;
cout<<"enter second number : ";
cin>>num2;
cout<<"enter third number : ";
cin>>num3;
cout<<"\t Program to find smallest number among three number\n";
if(num1<=num2)
{
if(num1<num3)
{
cout<<num1<<" is smallest number ";
}
else
{
cout<<num3<<" is smallest number ";
}
}
else if(num2<=num1)
{
if(num2<num3)
{
cout<<num2<<" is smallest number ";
}
else
{
cout<<num3<<" is smallest number ";
}
}
}
Output:
enter first number : 12
enter second number : 22
enter third number : 23
Program to find smallest number among three number
12 is smallest number
OR
By using a new variable smallest
using namespace std;
int main()
{
int x,y,z,smallest;
cout<<"enter first number ";
cin>>x;
cout<<"enter second number ";
cin>>y;
cout<<"enter third number ";
cin>>z;
smallest=x;
if(y<smallest)
smallest=y;
if(z<smallest)
smallest=z;
cout<<"\nsmallest number is : "<<smallest;
return 0;
}
Output:
enter first number 12
enter second number 22
enter third number 3
smallest number is : 3
Comments
Post a Comment