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 1 of 3.
Puneeth said:
7 years ago
If we print k what is the answer.
(3)
Sweety said:
9 years ago
I think 200 is the correct answer.
(3)
Karthik said:
9 years ago
@ALL.
Look at this carefully. They asked only the value of num not the value of k.
Look at this carefully. They asked only the value of num not the value of k.
(1)
Amit said:
1 decade ago
@Thirupathi.
Skip this statement k = (num>5 ? (num <=10 ? 100 : 200) : 500) ;
Because we have to print the value of num not the value of k.
So it will print num=30.
Thanks.
Skip this statement k = (num>5 ? (num <=10 ? 100 : 200) : 500) ;
Because we have to print the value of num not the value of k.
So it will print num=30.
Thanks.
(1)
Luis said:
1 decade ago
Ternary operators work like this:
int a,b,c;
a = (condition) ? value if true : value if false;
In this example there is a nested ternary operation, but the question was tricky.
int a,b,c;
a = (condition) ? value if true : value if false;
In this example there is a nested ternary operation, but the question was tricky.
(1)
Teja said:
1 decade ago
#include <stdio.h>
int main() {
int a=400,b=700;
if(!a<=500)
{
b=80;
c=90;
printf("%d %d",b,c);
}
return 0;
}
How does the condition works with a works? Can u please help me?
int main() {
int a=400,b=700;
if(!a<=500)
{
b=80;
c=90;
printf("%d %d",b,c);
}
return 0;
}
How does the condition works with a works? Can u please help me?
Arch said:
2 years ago
if k= 100%1000 print("k=",k) find the value of k.
Anyone, please solve this.
Anyone, please solve this.
Punam solanki said:
2 years ago
Anyone, Explain the K variable condition?
Princess said:
4 years ago
I want ask a doubt; plz explain me.
num =5.
num=num.
print(num).
num =5.
num=num.
print(num).
Arvi said:
6 years ago
What will be the value of k if num is 20?
k = (num > 50 ? 100: 500);
Please anyone explain me.
k = (num > 50 ? 100: 500);
Please anyone explain me.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers