// Online C++ compiler to run C++ program online #include <iostream> #include<iomanip> using namespace std; int main() { //Write a C++ program that takes three numbers as input and uses the ternary operator to find and print the maximum of the three numbers. int n1,n2,n3,n4,max,max2,d; char invalid; cout<<"enter four numbers"<<endl; cin>>n1>>n2>>n3>>n4; max = (n1>n2)?(n1>n3)?(n1>n4)?n1:n2:((n3>n2)?n3:n2):(n4>n3)?n4:n3; cout<<"max value is "<<max<<endl; max2 = (max>n1)?n1:(max>n2)?n2:(max>n3)?n3:(max>n4)?n4:invalid; cout<<"2nd max value is "<<max2<<endl;
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter