Assignment 5 July 2022 Solution
Assignment 5 July 2022 Solution
a)10 9 8 7 6 5 4 3 2 1
b) 1 2 3 4 5 6 7 8 9 10
c) No output
d) Error
Solution: (c)The test condition is not proper at the beginning. So, loop will not
execute.
Solution: (d) ‘do-while’ loop is not always an infinite loop. It depends on the
condition mentioned inside the while statement.
‘do-while’ loop is an exit-controlled loop.
4. Compute the printed value of ‘m’ and ‘n’ of the C program given below
Week 5 Assignment Solution
#include <stdio.h>
int main()
{
int m= 0, n = 0;
while (m<5, n <7)
{
m++;
n++;
}
printf("%d, %d\n", m, n);
return 0;
}
a) 5,7
b) 5,5
c) 7,7
d) 0,0
Solution: (c) The while condition checks the last condition (i.e. n<7) and till the
condition is satisfied the block inside the loop is executed. Thus the loop is run for
7 times and both the values of m and n are incremented by 7.
5. What should be in the place of ****** so that except i=8, rest of the values of i
(as defined in the ‘for’ loop: i=0 to i=19) will be printed?
#include <stdio.h>
int main()
{
int i = 0;
for (i = 0;i< 20; i++)
{
if(i==8)
{
**********;
}
printf("i=%d\n",i);
}
return 0;
}
a) break
b) continue
c) switch
d) exit
Week 5 Assignment Solution
Solution: (b) continue forces the next iteration of the loop to take place skipping
any code in between.
a) NPTEL
b) IIT/IISc
c) NPTELSWAWAMIIT/IISc
d) Compilation error
Solution: (c) Here no ‘break’ statement is given. When a match is found, the
program executes the statements following that case, and all subsequent case and
default statements as well.
7. What will be the output?
#include <stdio.h>
int main()
{
int k,j;
for (k=1,j=3;k<=3,j>=1;k++,j--)
{
printf("%d,%d\n",k,j);
}
return 0;
}
a) 1,3
b) 1,3
3,1
c) 1,3
2,2
3,1
d) 0,0
Week 5 Assignment Solution
Solution: (c) Multiple variables can be initialized as well as multiple condition ,and
propagation can be made in a single ‘for’ loop just by using a comma ‘,’.So k value
will be increased upto 3 and j value will be decreased to 1.
Solution: (b) for loop will be continue for 3 times(p=0,1,2) and 4 will be print three
times. Here, the variable ‘p’ inside the for loop is a local variable. So, this local
variable does not change the condition of the for loop.
9.
For the C program given below, if the input given by the user is 7. What will be
shown on the output window?
#include <stdio.h>
int main()
{
int n,i=2;
scanf("%d",&n);
do
{
if(n%i==0)
{
printf("The number is odd");
}
i++;
}
while(i<n);
printf("The number is prime");
return 0;
}
Week 5 Assignment Solution
a) The number is odd
b) The number is prime
c) The number is odd The number is prime
d) Syntax Error
10. What will be the output? (Answer according to the output you get on the output window and the
options provided, though it prints some values previously.)
#include <stdio.h>
int main()
{
int i=0;
for(;;)
{
if(i==10)
continue;
printf("%d ",++i);
}
return 0;
}
a) 0 1 2 3 4 5 6 7 8 9 11 12…..infinite times
b) 1 2 3 4 5 6 7 8 9 11 12….infinite times
c) Won’t print anything
d) Error