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 > 5 ? ( num <= 10 ? 100 : 200 ) : 500 ) ;printf ( "\n%d", num ) ;}
Solution
The output of the program will be 30.
Here's the step by step explanation:
- The program starts with the main function.
- Inside the main function, two integer variables 'k' and 'num' are declared. 'num' is initialized with the value 30.
- 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.
- The printf statement prints the value of 'num', which is 30.
- The program ends.
Similar Questions
What will be the output of the following C code #include <stdio.h> int main() { int x = 20; x %= 3; printf("%d",x); return 0; }
What will be the output of the following program?#include<stdio.h> int X=40; int main() { int X=20; printf("%d", X); return 0; }Select one:400Error20
hat will be the output of the following code snippet?123456#include <stdio.h>int main() { int num = 20; printf("%d", num >= 20 && num <= 30); return 0;}
What is The Output of the following program? #include<stdio.h> void main() { int i = printf("00"); int i =printf("00"); printf("%d",i); } 0 00 002
What will be the output of the C program?#include<stdio.h>int main(){ char num = '\010'; printf("%d", num); return 0;}a.10b.8c.10d.8
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.