Computer 12 CH12MCQs
Computer 12 CH12MCQs
(a) 5 (b) 6
(c) 0 (d) None
[Ch#12] Loop Constructors 359 Computer Science Part-II
36. What will be the output of the following code segment?
int n = 5;
while (n > 5)
n * = 10;
printf(“%d”, n );
(a) 5 (b) 10
(c) 50 (d) 0
37. What will be the output of the following code segment?
int m = 10, n = 100;
do
{
m = m + (n ++);
m ++ ;
}while (m < 10);
printf(“%d”, m );
(a) 10 (b) 110
(c) 111 (d) 112
38. What will be the output of the following code segment?
int x = 0;
for (x = 100; x = = 200; x ++);
printf(“%d”, x );
(a) 101 (b) 100
(c) 201 (d) 200
39. What will be the output of the following code segment?
int n = 0
for (;;)
{
if (n = = 10) break;
n++;
}
printf(“%d”, n );
(a) 0 (b) 10
[Ch#12] Loop Constructors 360 Computer Science Part-II
(c) 11 (d) 12
40. What is the final value of x when the code intx; for(x=0; x<10; x++) is run?
(a) 10 (b) 9
(c) 0 (d) 1
Q.3 Write T for true and F for false statements.
1. There is no difference between while and do-while loop. (F)
2. The body of while loop may or may not execute. (T)
3. The do-while loop always executes at least once. (T)
4. The var++ is an example of prefix increment operator. (F)
5. The condition of an infinite loop never becomes true. (F)
6. Initialization expression is option in for loop. (T)
7. The for(i = 1; i < = 10; i++); is an infinite loop. (F)
8. Loop is a decision making construct. (F)
9. A while loop can not be used in the body of a for loop. (F)
10. In type casting, a variable of one type behaves as the variable of (T)
another type temporarily.
Programs using loops:
Q4. To print and find the sum of 10 natural numbers (1+2+3+ -----) using for loop
Answer:
#include<stdio.h>
void main ( )
{
int n,sm=0;
for(n=1;n<=10;n++)
{
printf(“\n %d”,n);
sm=sm+n;
}
printf(“\n The sum of 10 natural numbers = %d”,sm);
}
Q5. Print and find the sum of 10 numbers which are multiple of 5 (5+10+15+ -----
--) using for loop
Answer:
#include<stdio.h>
void main ( )
{
int n, sm=0;
for(n=5;n<=50;n+=5)
[Ch#12] Loop Constructors 361 Computer Science Part-II
{
printf(“\n %d”,n);
sm=sm+n;
}
printf(“\n The sum of numbers = %d”, sm);
}
Q6. Program: Print and find the sum of 10 natural numbers (1+2+3+ ---------)
Using While loop
Answer:
#include<stdio.h>
void main ( )
{
int n=1,sm=0;
while(n<=10)
{
printf(“\n %d”,n);
sm=sm+n;
n++;
}
printf(“\n The sum of 10 natural numbers = %d”,sm);
}
Q7. Print and find the sum of 15 even numbers (2+4+6+ -----) using do-while loop
Answer:
#include<stdio.h>
void main ( )
{
int n=2,sm=0;
do
{
printf(“\n %d”,n);
sm=sm+n;
n+=2;
}
while(n<=30);
printf(“\n The sum of 15 even numbers = %d”,sm);
[Ch#12] Loop Constructors 362 Computer Science Part-II
}
Q8. Print the table of any number using for loop
Answer:
#include<stdio.h>
void main ( )
{
int n,c;
printf(“\n Enter table Number “);
scanf(“%d”, &n);
for(c=1;c<=10;c++)
{
printf(“\n %d x %d = %d”, n, c , n*c);
}
}
Q9. Print the table of any number using while loop
Answer:
#include<stdio.h>
void main ( )
{
int n,c;
printf(“\n Enter table Number “);
scanf(“%d”, &n);
c=1;
while(c<=10)
{
printf(“\n %d x %d = %d”, n, c ,n*c);
c++;
}
}
Q10. Print the Table using do-while loop
Answer:
#include<stdio.h>
void main ( )
{
int n, c;
[Ch#12] Loop Constructors 363 Computer Science Part-II
printf(“\n Enter table Number “);
[Ch#12] Loop Constructors 364 Computer Science Part-II
scanf(“%d”, &n);
c=1;
do
{
printf(“\n %d x %d = %d”, n , c, n*c);
c++;
}
while(c<=10);
}
Q11. Print the factorial of a number using do while
Answer:
#include<stdio.h>
void main ( )
{
int n,c=1;
printf(“\n Enter any Number 0 to 7 “);
scanf(“%d”, &n);
do
{
c=c*n;
n--;
}
while(n>=1);
printf(“\n The Factorial of given number = %d “, c);
}
Q12. Print the factorial of a number using for loop
Answer:
#include<stdio.h>
void main ( )
{
[Ch#12] Loop Constructors 365 Computer Science Part-II
int n,c=1,k;
printf(“\n Enter any Number 0 to 7 “);
scanf(“%d”, &n);
for(k=n;k>=1;k--)
{
c=c*k;
}
printf(“\n The Factorial of given number = %d “, c);
}
Q13. Trace the output of the program:
Answer:
Program Output ( � denotes blank spaces)
k=0; loop iteration = 6
while(k<=5) ( k=0,1,2,3,4,5)
{ k=0 ; output � �0 � 10
printf(“%3d%3d\n”, k , 10-k); k=1 ; output � �1 � �9
k++; k=2 ; output � �2 � �8
}
k=3 ; output � �3 � �7
k=4 ; output � �4 � �6
k=5 ; output � �5 � �5
Q14. Trace the output of the program
Answer:
Program Output ( _ denotes blank spaces)
j=10; loop iteration = 5
for(int i=1;i<=5; + + i) (i=,1,2,3,4,5)
{ i=1 ; output 1 _ 10
printf(“%d %d\n”, i, j); i=2 ; output 2 _ 8
j - = 2; i=3 ; output 3 _ 6
} i=4 ; output 4 _ 4
i=5 ; output 5 _ 2
[Ch#12] Loop Constructors 366 Computer Science Part-II
Q15. Trace the output of the program assuming m=3,n=5
Answer:
Program Output
for(k=1;k<=n; ++k) *
{ **
for(j=0; j<k; ++j) ***
{ ****
printf(“*”); *****
}
printf(“\n”);
}
Q16. Trace the output of the program assuming m=3,n=5
Answer:
Program Output
for(k=n;k>=0; --k) ***
{ ***
for(j=m; j>0; --j) ***
{ ***
printf(“*”); ***
}
printf(“\n”);
}
Q17. Correct the following code according to the instructions: Insert braces where
they are needed and correct errors if any. The corrected code should accept
five integers and should display their sum.
Answer:
incorrect code correct code
count = 0; int count=0, sum=0, next_num;
while(count<=5); count = 0;
count += 1; while(count<=5)
printf(“next number >”); {
scanf(“%d”, &next_num); count += 1;
next_num += sum; printf(“next number >”);
printf(“%d numbers were added; \n”); scanf(“%d”, &next_num);
printf(“their sum is %d. \n”, sum); sum += next_num;
}
printf(“%d numbers were added; \n”,
count);
[Ch#12] Loop Constructors 367 Computer Science Part-II
printf(“their sum is %d. \n”, sum);
Q18. Rewrite the following code segment using do-while loop.
Answer:
Code with do-while statement
sum=0; int sum=0,odd=1;
for(odd = 1; odd<n; odd = odd+2) do
sum = sum + odd; {
printf(“sum of the positive odd numbers less sum = sum + odd;
than %d is %d\n”,n, sum); odd = odd+2;
}
while(odd<=n);
printf(“sum of the positive odd numbers less
than %d is %d\n”,n, sum);
Q19. Prime Number
Answer:
#include<stdio.h>
void main ( )
{
int n, k;
printf(“\n Enter any Number :>>>> “);
scanf(“%d”, &n);
k=2;
while (k<=n-1)
{
if(n%k==0)
{
printf(“\n %d is not A Prime Number “, n);
break;
}
k++;
}
if (k == n)
[Ch#12] Loop Constructors 368 Computer Science Part-II
printf(“\n The Number is prime = %d “, n);
}
Q20. Print first 10 Natural Numbers, their squares and their cubes and calculate
the sum of these three series using for loop.
S1=1+2+3+-----+10, S2=12+22+32+-----+102, S3=13+23+33+-----+103
Answer:
#include<stdio.h>
void main( )
{
int i, s1=0,s2=0,s3=0;
for(i=1;i<=10;i++)
{
printf(“\n %d \t %d \t %d”,i,i*i,i*i*i);
s1=s1+I;
s2 = s2 + (i*i);
s3 = s2 + (i*i*i);
}
printf(“\ns1= %d \t s2=%d \t s3=%d”, s1,s2,s3);
}
Q21. Write a program that produces the following output
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
Answer:
#include<stdio.h>
void main ( )
{
int i, j;
for (j=0;j<=5;j++)
{
for(i=0;i<=j;i++)
printf(“%d\t”,i);
[Ch#12] Loop Constructors 369 Computer Science Part-II
printf(“\n”);
}
}
Q22. Write a program that produces the following output
0 1
1 2
2 4
3 8
4 16
5 32
6 64
Answer:
#include<stdio.h>
#include<math.h>
void main ( )
{
float j;
for (j=0;j<=6;j++)
printf(“\n%4.0f\t\t%4.0f”, j, pow(2,j) );
}
OR
#include<stdio.h>
void main ( )
{
int j, i = 1;
for (j=0;j<=6;j++)
{
printf(“\n%d\t%d”, j, i);
i *= 2;
}
}
Q23. Input and output of an array using scanf( )
Answer:
#include<stdio.h>
void main( )
{
int a[5],i;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element ”);
scanf(“%d”, &a[i]);
[Ch#12] Loop Constructors 370 Computer Science Part-II
}
for(i=0;i<=4;i++)
printf(“\n Array Element at %d index = %d ”,i, a[i]);
}
Q24. Input array elements and calculate sum
Answer:
#include<stdio.h>
void main( )
{
int a[5],i, s=0;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element ”);
scanf(“%d”, &a[i]);
s += a[i];
}
printf(“\n Sum of all Array Elements = %d ”, s);
}
Q25. Input array elements and sorting of an array.
Answer:
#include<stdio.h>
void main( )
{
int a[5], i, temp, j;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element ”);
scanf(“%d”, &a[i]);
}
for(i=1;i<=4;i++)
for(j=0;j<=3;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
[Ch#12] Loop Constructors 371 Computer Science Part-II
}
for(i=0;i<=4;i++)
printf(“\n Array Elements in order are : %d”, a[i]);
}
Q26. Input array elements and find largest number in an array.
Answer:
#include<stdio.h>
void main( )
{
int a[5], i, max;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element”);
scanf(“%d”, &a[i]);
}
max = a[0];
for(i=1;i<=4;i++)
if(a[i]>max)
max=a[i];
printf(“\n Largest Number in Array Elements = %d ”, max);
}
Q27. Input array elements and find smallest number in an array.
Answer:
#include<stdio.h>
void main( )
{
int a[5], i, min;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element”);
scanf(“%d”, &a[i]);
}
min=a[0];
for(i=1;i<=4;i++)
if(a[i]<min)
min=a[i];
[Ch#12] Loop Constructors 372 Computer Science Part-II
printf(“\n Smallest Number in Array Elements = %d ”, min);
}