C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Find Output of Program (Q.No. 11)
11.
What will be the output of the program?
#include<stdio.h>
int main()
{
int k, num = 30;
k = (num < 10) ? 100 : 200;
printf("%d\n", num);
return 0;
}
Discussion:
39 comments Page 1 of 4.
Rajiya said:
1 decade ago
In the Above program we can't declear
k=(num>(num < 10 ? 100 : 200): 500);
we given only K=(num < 10) ? 100 : 200
There in program (num < 10) is false so
it has output 200
Please Explain me sir,Thank You.
k=(num>(num < 10 ? 100 : 200): 500);
we given only K=(num < 10) ? 100 : 200
There in program (num < 10) is false so
it has output 200
Please Explain me sir,Thank You.
Sudhakar said:
1 decade ago
In printf statement dey asked what is outpt of num. Not k. Here is explanation.
Here we want only output for num. So num=30.
If we want output of k then it is 200.
Here we want only output for num. So num=30.
If we want output of k then it is 200.
Venkat said:
1 decade ago
Yeah you're exactly correct sudhakar.
Rakesh said:
1 decade ago
Absolutely correct.
We have to read complete question, and first we have to understand logic.
We have to read complete question, and first we have to understand logic.
Anoo said:
1 decade ago
@sudhakar thank you.
KTR said:
1 decade ago
In the Above program the output is "30".
Because
-> There is no assignment statement for "num" other than "num=30"@ line 3
-> "k = (num < 10) ? 100 : 200;"
This expression will assign the value for "K" only not for num.
-> We are printing the value of "num" not "K"
Because
-> There is no assignment statement for "num" other than "num=30"@ line 3
-> "k = (num < 10) ? 100 : 200;"
This expression will assign the value for "K" only not for num.
-> We are printing the value of "num" not "K"
VANI said:
1 decade ago
Please explain this logic.
(num < 10) ? 100 : 200;.
(num < 10) ? 100 : 200;.
Meenu said:
1 decade ago
It means that if num is less than 10 then 100 is assigned else 200 is assigned
i.e, in ternary operator if condition is true one statement is executed else other is executed .
Syntax:
(condition)?stmt1:stmt2;
If conditon is true stmt1 is executed else stmt2.
i.e, in ternary operator if condition is true one statement is executed else other is executed .
Syntax:
(condition)?stmt1:stmt2;
If conditon is true stmt1 is executed else stmt2.
Anurag sharma said:
1 decade ago
It's a simple program in which we just assign a value to k by checking conditional operator & condition comes to true.
So we have k=100 but we print num which is still 30 so 30 will print.
So we have k=100 but we print num which is still 30 so 30 will print.
Prasad said:
1 decade ago
Here num=30 is assigned the value when you compare proparly 30<10 it false No than he willbe stop and they cannot exicuted the next statment and than 30 will be print.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers