Knowee
Questions
Features
Study Tools

What will be the output of following program?main( ){int k, num = 30 ;k = ( num > 5 ? ( num <= 10 ? 100 : 200 ) : 500 ) ;printf ( "\n%d", num ) ;}

Question

What will be the output of following program?

main( ) { 
    int k, num = 30 ;
    k = ( num &gt; 5 ? ( num &lt;= 10 ? 100 : 200 ) : 500 );
    printf ( &quot;\n%d&quot;, num );
}
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the program will be 30.

Here's the step by step explanation:

  1. The program starts with the main function.
  2. Inside the main function, two integer variables 'k' and 'num' are declared. 'num' is initialized with the value 30.
  3. The next line is a ternary operator which is a shorthand for an if-else statement. It checks if 'num' is greater than 5. If it is, it checks if 'num' is less than or equal to 10. If 'num' is less than or equal to 10, 'k' is assigned the value 100. If 'num' is greater than 10, 'k' is assigned the value 200. If 'num' is not greater than 5, 'k' is assigned the value 500. In this case, since 'num' is 30, 'k' is assigned the value 200.
  4. The printf statement prints the value of 'num', which is 30.
  5. The program ends.

This problem has been solved

Similar Questions

What will be the output of the following C code?#include <stdio.h>int main() {    int i;    for (i = 0; i < 5; i++);    printf("%d\n", i);    return 0;}

find the output of given code.#include <stdio.h>int main(){int a,b=110,c=20;printf("%d",a=b-=c*=5);return 0;}

What is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}

The output of the code will be ____. int i=5; printf(“%d %d %d”, i, i++, ++i); in c

What will be the output of the following code?#include <stdio.h>int main() {    int arr[4] = {0, 1, 2, 3};    printf("%d\n", arr[-1]);    return 0;}

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.