Java Program To Demonstrate Ternary Operator

Write Java program to demonstrate the use of the ternary operator

// WAP to demonstrate the use of ternary operator

import java.util.Scanner;
public class TernaryOperator
{
    public static void main(String []args)
    {
        int age;
        
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter your age :-->");
        age = sc.nextInt();

        boolean b =(age >= 18) ? true:false;

        if(b)
        {
            System.out.println("You are eligible to vote");
        }
        else
        {
            System.out.println("You are not eligible to vote");
        }

        sc.close(); // to prevent memory leakage


    }
}

Output:


<< Previous | Next >>


Leave a Reply

Your email address will not be published.