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.
Chetan said:
7 years ago
Because here condition is true but num variable store in 30 value is not changed. So answer print is num value.
(5)
Kundana said:
8 years ago
1) int num=1; / initialized 'num' variable/.
2)k= (num<10) ? 100 : 200; // k= (30<10) if statement true then print. ("100") if statement is false then print ("200") //
3) But in program, they asked to print 'num' which is already initialized (int num = 30) so will be printed.
2)k= (num<10) ? 100 : 200; // k= (30<10) if statement true then print. ("100") if statement is false then print ("200") //
3) But in program, they asked to print 'num' which is already initialized (int num = 30) so will be printed.
(3)
Saloni kamboj said:
9 years ago
k = (num < 10) ? 100 : 200;
I am not getting this statement, Please explain it to me.
I am not getting this statement, Please explain it to me.
(2)
Varsha said:
5 years ago
If (30<10) 200 is executed. If that statement true means 100 is executed. So the answer is 200.
(1)
Rathan said:
1 decade ago
In this program:
int main()
{
int k, num = 30;
k = (num < 10) ? 100 : 200;
printf("%d\n", num);
return 0;
}
Here 30<10 condition is false.
k=200 but in printf function asked the value of 'num'.
So output is 30.
int main()
{
int k, num = 30;
k = (num < 10) ? 100 : 200;
printf("%d\n", num);
return 0;
}
Here 30<10 condition is false.
k=200 but in printf function asked the value of 'num'.
So output is 30.
(1)
Sandy said:
9 years ago
Step 1: int k, num=30; // Here variable num is initialized to '30',
Variable k is declared, but not initialized.
Step 2: k = (num < 10) ? 100 : 200; // Is nothing but if(30 < 10) k = 100; else k=200;
Hence this condition will be failed. And k value becomes 200 (k = 200).
Step 3: printf("%d\n", num); // Here it is printing num value but not asked about to print k value.
So, finally will get output 30.
Variable k is declared, but not initialized.
Step 2: k = (num < 10) ? 100 : 200; // Is nothing but if(30 < 10) k = 100; else k=200;
Hence this condition will be failed. And k value becomes 200 (k = 200).
Step 3: printf("%d\n", num); // Here it is printing num value but not asked about to print k value.
So, finally will get output 30.
(1)
P.kranthi kumar said:
9 years ago
k = (num<10)?100:200 is become false&
Here print the k, so the answer will become 30.
Here print the k, so the answer will become 30.
Reddy'Siva said:
1 decade ago
Here the question is about the value of the variable "num" and not "k" that's why the answer is 30 good thing.
Rohan Dutta said:
1 decade ago
Here the question is about the value of "num", not "k". When we are using the condition the value of "k" has been changed. But the value of "num' remains unchanged. Therefore the output is num=30.
Jignesh patel said:
1 decade ago
In this programme conditional operators process does not assign value to number so output of num remains same as it is so num=30 only assignment operator can change value of number variable.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers