Friday, September 6, 2013

Conditional operator

The conditional operator evaluates an expression returning a 
value if that expression is true and a different one if the expression is evaluated as false. Its format is:

condition ? result1 : result2

If condition is true the expression will return result1, if it is not it will return result2.

-
7 == 5 ? 4 : 3 // returns 3, since 7 is not equal to 5.

7 == 5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2.


5 > 3 ? a : b // returns the value of a, since 5 is 
greater

 than 3.

a > b ? a : b // returns whichever is greater, a or b.


here a program written using condition operator

#include<iostream>
using namespace std;
int main()
{
   int a,b,c;
   cout<<" enter 2 numbers\n";
   cin>>a>>b;
   c=(a>b)?a:b;
   cout<<" the greatest number is\n"<<c;
   return 0;
}



















 let us see how to write a program to find a number is

odd or even























now let us see how to write a program to find a year leap 

year or not

















checking whether the digit is one digit,two digit,three digit

























to find the greatest number using if and condition operator and using nested if else
#include<iostream>
using namespace std;
int main()
{
int n1,n2,n3,a;
cout<<"enter three numbers";
cin>>n1>>n2>>n3;
if(n1>n2)
{
a=(n1>n3)?n1:n3;
          }
else
{
a=(n2>n3)?n2:n3;
          }
cout<<"the greatest number is "<<a;
return 0;
}


























using nested  if else
#include<iostream>
using namespace std;
int main()
{
int a,b,c,g;
cout<<" enter three numbers";
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
  g=a;
  else
  g=c;
}
else
          {
if(b>c)
g=b;
else
g=c;
}
cout<<" the greatest number is "<<g;
return 0;
}

if you want more give me a feedback in the comment

0 comments:

Post a Comment