"Z Code Writer" is an Online Blog for Programmer & Beginner. Here, You will Find tech, Build your Programming Skills and other Programming Language Like C, C++, HTML, Java Script Java Code, Web Designing and University Of Sialkot (USKT) CGPA / GPA Calculator etc.

Saturday 27 July 2019

Conditional Operator

Conditional Operator

      The conditional operator is also used for making two-way decisions. It can be used in place of simple 'if-else' structure. It is denoted by and signs. Three operands are used with conditional operator. It is also called ternary operator.      The general syntax of conditional operator is as follows:
                    (condition)   ?  exp1  :  exp2

Write a program that inputs two numbers and displays greater number using conditional operator.


Example in C


#include < stdio.h >
#include < conio.h >
void main ( )
{
      int a , b ;
      clrscr ( ) ;
      printf ( " Enter first Number : " ) ;
      scanf ( " %d " , &a ) ;
      printf ( " Enter second Number : " ) ;
      scanf ( " %d " , &b ) ;
     ( a > b ) ? printf ( " %d is greater " , a ) : printf ( " %d is greater " , b ) ;
      getch ( ) ;
}

Example in C++


#include < iostream >
using namespace std;
int main ( )
{
      int a , b ;
      cout << " Enter first Number " ;
      scanf >> a ;
      cout << " Enter second Number " ;
      scanf >> b ;
     ( a > b ) ? cout << a << " is greater " : cout << b << " is greater " ;
      retirn 0 ;
}



Output of the program
Enter first Number: 5
Enter second Number: 15
15 is greater 

No comments:

Post a Comment

Thanks For Visiting Here...!! I will Reply you as soon as possible.