ProgrammingforOutputProblems
October 6, 2023ProgrammingforOutputProblems
October 6, 2023ProgrammingforOutputProblems
Question 10

Consider the following C code.
#include
#include
void main()
{
double pi = 3.1415926535;
int a = 1;
int i;
for(i=0; i < 3; i++)
if(a = cos(pi * i/2) )
printf(“%d “,1);
else printf(“%d “, 0);
}
What would the program print?
#include
#include
void main()
{
double pi = 3.1415926535;
int a = 1;
int i;
for(i=0; i < 3; i++)
if(a = cos(pi * i/2) )
printf(“%d “,1);
else printf(“%d “, 0);
}
What would the program print?
000


010


101


111

Question 10 Explanation:
→The for loop will execute three times for a given i values 0,1,2.
→For a given i = 0:
a = cos(pi * 0/2) [ PI*0 is 0]
a = cos(0) = 1, if condition true and it will print 1
→For a given i = 1
a = cos (pi/2) [pi*1 is pi]
a=cos(1.57075) whose value approximately equal to zero
a = 0, Here the condition is false then else part will execute and it will print 0
→For i = 2
a = cos(pi) [pi*2/2 is nothing but pi]
a = 1, ,Here also condition is true and it will execute if part and it will print “1”
→Finally it will print 101.
→For a given i = 0:
a = cos(pi * 0/2) [ PI*0 is 0]
a = cos(0) = 1, if condition true and it will print 1
→For a given i = 1
a = cos (pi/2) [pi*1 is pi]
a=cos(1.57075) whose value approximately equal to zero
a = 0, Here the condition is false then else part will execute and it will print 0
→For i = 2
a = cos(pi) [pi*2/2 is nothing but pi]
a = 1, ,Here also condition is true and it will execute if part and it will print “1”
→Finally it will print 101.
Correct Answer: C
Question 10 Explanation:
→The for loop will execute three times for a given i values 0,1,2.
→For a given i = 0:
a = cos(pi * 0/2) [ PI*0 is 0]
a = cos(0) = 1, if condition true and it will print 1
→For a given i = 1
a = cos (pi/2) [pi*1 is pi]
a=cos(1.57075) whose value approximately equal to zero
a = 0, Here the condition is false then else part will execute and it will print 0
→For i = 2
a = cos(pi) [pi*2/2 is nothing but pi]
a = 1, ,Here also condition is true and it will execute if part and it will print “1”
→Finally it will print 101.
→For a given i = 0:
a = cos(pi * 0/2) [ PI*0 is 0]
a = cos(0) = 1, if condition true and it will print 1
→For a given i = 1
a = cos (pi/2) [pi*1 is pi]
a=cos(1.57075) whose value approximately equal to zero
a = 0, Here the condition is false then else part will execute and it will print 0
→For i = 2
a = cos(pi) [pi*2/2 is nothing but pi]
a = 1, ,Here also condition is true and it will execute if part and it will print “1”
→Finally it will print 101.
Subscribe
Login
0 Comments