C++ Program To Demonstrate Ternary Operator

C++ Program To Demonstrate Ternary Operator

Write C++ code to demonstrate the use of ternary operator

// Write C++ code to demonstrate the use of ternary operator

#include <bits/stdc++.h>
using namespace std ;

int main()
{
    int age ;

    cout << "Enter your age :--> ";
    cin >> age ;

    (age >= 18) ? cout << "You are eligible to vote"  : cout << "You are not eligible to vote" ;
    //if age greater than 18 then eligible else not

    return 0 ;
}

Output:

Run - 1:
Enter your age :--> 56
You are eligible to vote

Run - 2:
Enter your age :--> 12
You are not eligible to vote