Write a program to find the greatest of three numbers using ternary operators in C++
In this, we are going to see a program to  find the greatest of three numbers using Ternary Operators in C++ Programming Language.



The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <conio.h>


void main()
{
    clrscr();
	int a, b, c;
	cout<<"Enter three numbers: ";
	cin>>a>>b>>c;
	(a>b)?
	((a>c)?cout<<"Greatest is: "<<a:cout<<"Greatest is: "<<c)
	:((b>c)?cout<<"Greatest is: "<<b:cout<<"Greatest is: "<<c);
	getch();
}

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;
int main()
{
	int a, b, c;
	cout<<"Enter three numbers: ";
	cin>>a>>b>>c;
	(a>b)?
	((a>c)?cout<<"Greatest is: "<<a:cout<<"Greatest is: "<<c)
	:((b>c)?cout<<"Greatest is: "<<b:cout<<"Greatest is: "<<c);
	return 0;
}


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post