Greatest number of three numbers in C++
#include<iostream>
using namespace std;
int main()
{
int x,y,z,greatest;
cout<<"enter first number ";
cin>>x;
cout<<"enter second number ";
cin>>y;
cout<<"enter third number ";
cin>>z;
greatest=x;
if(y>greatest)
greatest=y;
if(z>greatest)
greatest=z;
cout<<"\nlargest number is : "<<greatest;
return 0;
}
Output:
enter first number 5
enter second number 6
enter third number 9
largest number is : 9
Or
#include<iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"enter first number ";
cin>>x;
cout<<"enter second number ";
cin>>y;
cout<<"enter third number ";
cin>>z;
if(x>y && x>z)
{
cout<<"x is greater than y";
cout<<"x is greater than z";
}
else if(y>x && y>z)
{
cout<<"y is greater than x";
cout<<"y is greater than z";
}
else
{
cout<<"z is greater than y and x";
}
}
Output
enter first number 5
enter second number 6
enter third number 9
z is greater than y and x
OR
#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 greatest number among three number\n";
if(num1>=num2)
{
if(num1>num3)
{
cout<<num1<<" is greatest number ";
}
else
{
cout<<num3<<" is greatest number ";
}
}
else if(num2>=num1)
{
if(num2>num3)
{
cout<<num2<<" is greatest number ";
}
else
{
cout<<num3<<" is greatest number ";
}
}
}
output:
enter first number : 3
enter second number : 6
enter third number : 9
Program to find greatest number among three number
9 is greatest number
Comments
Post a Comment