Here is a C++ program to find largest among two numbers through nested if-else, through two if statements, using only if statements through conditional operator etc.
Method 1 : C++ program to find largest among three numbers using if statement only
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
if(a>b&&a>c)
cout<<a<<" is greater ";
if(b>a&&b>c)
cout<<b<<" is greater ";
if(c>a&&c>b)
cout<<c<<" is greater ";
return 0;
}
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
if(a>b&&a>c)
cout<<a<<" is greater ";
if(b>a&&b>c)
cout<<b<<" is greater ";
if(c>a&&c>b)
cout<<c<<" is greater ";
return 0;
}
Method 2 : C++ program to find largest among three numbers using only two if statement
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
if(a>b)
b=a;
if(b>c)
c=b;
cout<<c<<" is greater ";
return 0;
}
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
if(a>b)
b=a;
if(b>c)
c=b;
cout<<c<<" is greater ";
return 0;
}
Method 3 : C++ program to find largest among three numbers using if-else ladder
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
if(a>b&&a>c)
cout<<a<<" is greater ";
else if(b>c)
cout<<b<<" is greater ";
else
cout<<c<<" is greater ";
return 0;
}
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
if(a>b&&a>c)
cout<<a<<" is greater ";
else if(b>c)
cout<<b<<" is greater ";
else
cout<<c<<" is greater ";
return 0;
}
Method 4 : C++ program to find largest among three numbers using if-else statement
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
cout<<a<<" is greater ";
else
cout<<c<<" is greater ";
}
else
{
if(b>c)
cout<<b<<" is greater ";
else
cout<<c<<" is greater ";
}
return 0;
}
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
cout<<a<<" is greater ";
else
cout<<c<<" is greater ";
}
else
{
if(b>c)
cout<<b<<" is greater ";
else
cout<<c<<" is greater ";
}
return 0;
}
Method 5 : C++ program to find largest among three numbers using bitwise operator
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
b=a>b?a:b;
c=b>c?b:c;
cout<<c<<" is greater ";
return 0;
}
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
b=a>b?a:b;
c=b>c?b:c;
cout<<c<<" is greater ";
return 0;
}
Method 6 : C++ program to find largest among three numbers using functions
#include<iostream>
using namespace std;
int grtr(int a,int b,int c)
{
b=a>b?a:b;
return c=b>c?b:c;
}
int main()
{
int a,b,c;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
cout<<grtr(a,b,c)<<" is greater ";
return 0;
}
using namespace std;
int grtr(int a,int b,int c)
{
b=a>b?a:b;
return c=b>c?b:c;
}
int main()
{
int a,b,c;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
cout<<grtr(a,b,c)<<" is greater ";
return 0;
}
No comments:
Post a Comment