Assignment Solution 5 Jan 2020
Assignment Solution 5 Jan 2020
a) 4, 5
b) 4, 4
c) 5, 5
d) 0, 0
Solution: (c) The while condition checks the last condition (i.e. j<5) and till the condition is satisfied the
block inside the loop is executed. Thus the loop is run for 5 times and both the values of i and j are
incremented by 5.
WEEK 5 ASSIGNMENT SOLUTION
a) n multiplied n times
b) factorial of n
c) display factors of n
d) display Fibonacci series upto n.
Solution: (b) In the for loop, 1 to n is multiplied. This computes the factorial of the number n.
a) IIT Delhi
b) IIT Kharagpur
c) IIT Madras
d) IIT Guwahati
Solution: (c)
WEEK 5 ASSIGNMENT SOLUTION
printf(“IIT”) prints IIT and counts the number of characters inside it which is 3 here. Therefore, the case 3
i.e. Madras will be printed next.
6. What will be the output?
#include <stdio.h>
int main()
{
if((0 && 1)||(1 && -1))
printf("Condition is true.");
else
printf("Condition is false.");
return 0;
}
a) Condition is true
b) Condition is false
c) Error
d) No output possible
Solution: (a)
(0 && 1) is 0, (1 && -1) is 1. (0 OR 1) is 1 hence if conditions is true and "Condition is true." is printed.
a) 1 2 3 4 5
b) 1 2 4 5
c) 1 2
d) 4 5
Solution: (c) Initially, c=1 which satisfies the while condition. It prints the value till c = 2. When c becomes
3 the if condition satisfies and the break the while loop. Therefore, 1 and 2 are printed.
char x=0;
for(x=0; x<=127; x++) {
printf("%d ", x);
}
return 0;
}
a) Compilation error
b) 0, 1, 2 …….., 127
c) 0, 1, 2, …….., 127, -128, -127,……., -2, -1, 0, 1, ……. infinite loop
d) 1, 2, 3…….,127
Solution: (c) The range of character variable is from -128 to 127. It is due to 1 Byte of memory allocation
i.e. -2^7 to 2^7-1. When x= 127, in the next iteration of the for loop, x is incremented to -128 that satisfies
the condition again. Therefore, the value of x runs in an infinite loop.
a) n==4
b) n%400 != 0
c) n>0
d) n%400 == 0
Solution: (d) This is the logic to decide whether a year is leap year or not.
10. What is the output of the following code?
#include <stdio.h>
int main()
{
int i=0;
do
{
printf("while vs do-while\n");
}while(i==0);
printf("Out of loop");
return 0;
}