Knowee
Questions
Features
Study Tools

Understanding Ternary operator#include <stdio.h>int main() { int k, num = 100; k = (num > 50 ? (num <= 10 ? 100 : 200): 500); printf("%d %d\n", num, k); return 0;}What are the values a =

Question

Understanding Ternary operator#include <stdio.h>int main() { int k, num = 100; k = (num > 50 ? (num <= 10 ? 100 : 200): 500); printf("%d %d\n", num, k); return 0;}What are the values a =

🧐 Not the exact question you are looking for?Go ask a question

Solution

The code you provided is written in C language. It uses a ternary operator, which is a shorthand way of writing an if-else statement. It has the form condition ? value_if_true : value_if_false.

Here's how the ternary operator in your code works:

  1. It first checks if num > 50. If this condition is true, it moves on to the next condition (num <= 10 ? 100 : 200). If it's false, it directly assigns 500 to k.

  2. The next condition num <= 10 ? 100 : 200 is checked only if the first condition num > 50 is true. If num <= 10, it assigns 100 to k, otherwise it assigns 200 to k.

Given that num = 100, the first condition num > 50 is true. So it moves on to the next condition num <= 10 ? 100 : 200. Since num = 100 is not less than or equal to 10, it assigns 200 to k.

So, the values are num = 100 and k = 200.

This problem has been solved

Similar Questions

Write a short note on Ternary Operatorwith an illustration

Understanding Ternary operatorC language provides an operator to evaluate conditions. It is called a conditional operator (?:) or a ternary operator.Ternary operator needs exactly three operands to compute the result.The syntax for using a ternary operator is:condition? expression1 : expression2The condition should always evaluate to 1 (true) or 0 (false). If the condition evaluates to true, then expression1 is evaluated and its value is returned, otherwise expression2 is evaluated and its value is returned.Given below is an example demonstrating the usage of a conditional operator:int marks = 75, pass_marks = 50;(marks > pass_marks) ? printf("Passed C Certification") : printf("Failed C Certification"); Since the condition marks > pass_marks evaluates to true, the expression1 containing the printf statement in the above example prints "Passed C Certification"Click on Live Demo to understand the working of a ternary operator.Read the code given below and choose the correct option.#include void main() { int age = 12 , eligibility = 18; (age < eligibility) ? printf("Not eligible for voting\n") : printf("Eligible for voting\n");}OptionsEligible for votingNot eligible for votingcompilation errorNone of the above

Choose the correct syntax for Ternary Operator?Optionscondition : expression1 ? expression2condition ? expression1 : expression2condition : expression1 : expression2condition ? expression1 ? expression2

Java ternary operator sometimes called?Options: Pick one correct answer from belowRelational operatorLogical operatorConditional operatorNone of the above

Which of the following is ternary operator?a.None of theseb.Intersectionc.Uniond.Select

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.