C Programming - Expressions - Discussion
Discussion Forum : Expressions - Find Output of Program (Q.No. 13)
13.
What will be the output of the program?
#include<stdio.h>
int main()
{
int k, num=30;
k = (num>5 ? (num <=10 ? 100 : 200): 500);
printf("%d\n", num);
return 0;
}
Answer: Option
Explanation:
Step 1: int k, num=30; here variable k and num are declared as an integer type and variable num is initialized to '30'.
Step 2: k = (num>5 ? (num <=10 ? 100 : 200): 500); This statement does not affect the output of the program. Because we are going to print the variable num in the next statement. So, we skip this statement.
Step 3: printf("%d\n", num); It prints the value of variable num '30'
Step 3: Hence the output of the program is '30'
Discussion:
27 comments Page 3 of 3.
Mad said:
1 decade ago
Because of no change of k in the code the logic worked and it remain as k=30.
Ali Umar said:
1 decade ago
Hi Sunil your logic is totally wrong.......
Follow this
This is if else statement in Conditional operator. ? means if & : means else
num = 30;
k = (num > 5 ?(if) (num <= 10 ? 100 : 200): 500);//Question
If condition will true it will take 1st following value
k = (num <=10 ? 100 : 200); // First Step
If condition will not true it will take following value
K = 500;
Final result will not come in First step. So you come in second step & in this step apply same rule with the value of num
k = (num <=10 ? 100 : 200);(Here again apply if & else condition)
.'. num = 30;
So K = 200; Ans
Hope you will try to understand.
Follow this
This is if else statement in Conditional operator. ? means if & : means else
num = 30;
k = (num > 5 ?(if) (num <= 10 ? 100 : 200): 500);//Question
If condition will true it will take 1st following value
k = (num <=10 ? 100 : 200); // First Step
If condition will not true it will take following value
K = 500;
Final result will not come in First step. So you come in second step & in this step apply same rule with the value of num
k = (num <=10 ? 100 : 200);(Here again apply if & else condition)
.'. num = 30;
So K = 200; Ans
Hope you will try to understand.
Ali Umar said:
1 decade ago
Hi Krishna,
? means if & : means else.
There is some magic in the question. So don't calculate for k. May be any thing the value will store in k You have no need of that value. So leave that function. The value of num is already assigned in the program. So simply you have to print that value.
So x = 30 correct ans.
? means if & : means else.
There is some magic in the question. So don't calculate for k. May be any thing the value will store in k You have no need of that value. So leave that function. The value of num is already assigned in the program. So simply you have to print that value.
So x = 30 correct ans.
Jagan Mohan said:
1 decade ago
@ sunil:
Here the question is regarding the value of 'num'.
You are correct in the sense of value 'k'.
Hope u can understand it.....
Here the question is regarding the value of 'num'.
You are correct in the sense of value 'k'.
Hope u can understand it.....
Sunil kore said:
1 decade ago
k = (num>5 ? (num <=10 ? 100 : 200): 500);
here (num <=10 ? 100 : 200) evaluated first o/p=200
hence
k = (num>5 ? 200: 500);
30>5
so ans will be 200
here (num <=10 ? 100 : 200) evaluated first o/p=200
hence
k = (num>5 ? 200: 500);
30>5
so ans will be 200
Krishna said:
1 decade ago
What does '?' mark implies here?
Satish said:
1 decade ago
Can you elobrate your answer please.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers