Write a program which demonstrates the Ternary (?) operator.

Category : Java | Sub Category : Operator | By Prasad Bonam Last updated: 2020-09-28 13:41:33 Viewed : 497


(Operator)

Write a program which demonstrates the Ternary (?) operator.

Source Code: Ternary.java

/**

 * Write a program which demonstrates the Ternary (?) operator.

 */

 

public class Ternary { // Declare class name

     public static void main(String a[]) { // Main body of programme start from here.

           int i, k; // daclare two variables i & k.

           i = 10; // Initialize i

           k = i < 0 ? -i : i; // get absolute value of i.

           System.out.print("Absolute value of "); // print string line

           System.out.println(i + " is " + k); // this print the value of i and k.

           i = -10; // Initialize

           k = i < 0 ? -i : i; // get absolute value of i.

           System.out.print("Absolute value of "); // print string line

           System.out.println(i + " is " + k); // this show the value of i and k.

     } // Main body of programme ended

} // class ended

 Output: Absolute value of 10 is 10

Absolute value of -10 is 10





Search
Related Articles

Leave a Comment: