Menu
Topics Index
...
`

Print Student Result Using Ternary


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to print the student result as 'Distinction', 'Pass' and 'Fail' using only the ternary operator.
Note : If the student marks is less than 35 print Fail, if marks are greater than or equal to 60 print Distinction, if marks greater than or equal to 35 and less than 60 print Pass.
Use System.out.println or System.out.print for printing.

Input (Integer) Printed Output
18 Fail
34 Fail
35 Pass
40 Pass
60 Distinction
77 Distinction



class PrintStudentResultUsingTernary
{
    public static void main(String s[])
    {
        printResult(30);

    }


    public static void printResult(int marks)
    {
    }

}
Topic: Ternary Operator In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

here we need to use the nested ternary operator.
* take an String variable check if the marks>=65 than temp="Disctinction" if not check inner condition if(marks<35) than "Fail": "Pass"
String s=(marks>=60)?"Distinction":(marks<35)?"Fail":"Pass";

Posted by Uday Kumar    2015-03-02 06:51:27


here we have to use ternary operator.... to print pas fail or dstnctn.... so here we go wid the prgrm........ result=((marks<35)?fail:(marks<60?pass:distinction)) ........here is the ternary cndtn... first it will chek if marks <35 then it will print fail.... otherwise goes to else part and cheks marks<60 if yes print pass else marks>60 so print distinction....

Posted by Asma Mujtaba Khan    2015-03-02 06:57:14


Here we have to use ternary I.e. conditional operator ( ? :  )

---if marked are less than 35 then "FAlL" should be printed.

Else
We have to check if marks are greater than 60 then we have to print " Distinction" otherwise print " PASS".

---we can use nested if or switch, but we have to use ternary operator here.

Syntax:
Int res=(condition)?(execute if condition is true):  ( execute if condition is false).

String res=(marks<35)?"FAIL" :  (marks>60) ? " Distinction" : "PASS".

Posted by Mânïshå Mùlchåndânï    2015-03-02 07:03:38


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Second Correct Comment'. The 'lucky liker' is Shubham Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-03 05:07:29


© meritcampus 2019

All Rights Reserved.

Open In App