You are on page 1of 7

Program Output Check

1. Write the Output of the following program.


main( ){ 1
int x, y, m, n; 2
x = y = 10; 3
x += 1; y -= 1; 4
m = ++x; n = y--; 5
Printf(“M=%d\n N=%d\n X=%d\n Y=%d\n”, m--, n, ++x, y); 6
} 7

Ans:
M=12
N=9
X=13
Y=8

Explanation:
At line 3  x = 10 , y = 10

At line 4  x = 11 , y = 9

At line 5  m = 12, x = 12, n = 9, y = 8

At line 5  M = 12, m = 11, N = 9, n = 9, X = 13, x = 13, Y = 8, y = 8

2. Write the output of the following program.

main( ){
printf (“%d%d\n”, 32768, 32777);
printf (“%d\n”, ‘T’);
printf (“%c\n”, 97);
}

Ans: The output of the program is:

3276832777
84
a

Dr. Abu Nowshed Chy 1


Lecturer, Dept. of CSE, CU
3. Write the output of the following program.

#include<stdio.h>

main (void) {
int a = 20, b = 10, c, d;

c = ++a - b;
d = b++ +a;

printf ("a = %d b = %d c = %d d = %d", a, b, c, d);


printf ("\n %d", a%b);
printf ("\n %d", (c<d)?c:d);
printf ("\n %f", (float) (a/b) );

Ans:
a = 21 b = 11 c = 11 d = 31
10
11
1.000000

4. Write the output of the following program.

#include<stdio.h>

main( ) {
int x = 0, y = 5, i = 2;

while (i--) {
x += 1; y -= 1;
printf("X= %d \n Y= %d", x, y);
}
}

Ans:

X= 1
Y= 4 X= 2
Y= 3

Dr. Abu Nowshed Chy 2


Lecturer, Dept. of CSE, CU
5. Write the Output of the following program.

main( ){
int i=0, x=0;

do{
if ( i%5==0 ){
x++;
printf (“%d”, x);
}
++i;
} while (i<20);
}

Ans:
1234

Explanation:
Here, i=0 x=1
i=1
i=2
i=3
i=4
i=5 x=2
i=6
i=7
i=8
i=9
i=10 x=3
i=11
i=12
i=13
i=14
i=15 x=4
i=16
i=17
i=18
i=19

Dr. Abu Nowshed Chy 3


Lecturer, Dept. of CSE, CU
6. Write the output of the following program.

#include<stdio.h>
main() {
int n, m, p;

for (n = 1, m = 50; n<=m; n=n+1, m=m-1) {


p = m/n;
printf ("%d %d %d \n", n, m, p);
}
}

Ans:
n m p

1 50 50
2 49 24
3 48 16
4 47 11
5 46 9
6 45 7
7 44 6
8 43 5
9 42 4
10 41 4
11 40 3
12 39 3
13 38 2
14 37 2
15 36 2
16 35 2
17 34 2
18 33 1
19 32 1
20 31 1
21 30 1
22 29 1
23 28 1
24 27 1
25 26 1

Dr. Abu Nowshed Chy 4


Lecturer, Dept. of CSE, CU
7. Write the output of the following program.

#include<stdio.h>
main( ) {
int i, sum = 0;

for (i=1; i<20 && sum<100; ++i) {


sum = sum + i;
printf ("%d \n",sum);
}
}
Ans:

1
3
6
10
15
21
28
36
45
55
66
78
91
105

8. The following is a segment of a program:

y = 1;
if(x>=0)
if(x>0)
y = x + 1;
else
y = x - 1;

What will be the value of y if x equals to i) -2 ii) 0 and iii) 2 ?

Ans:

i) If x = - 2 then y = no ans / no value


ii) If x = 0 then y = -1
iii) If x = 2 then y = 3

Dr. Abu Nowshed Chy 5


Lecturer, Dept. of CSE, CU
9. Write the output of the following program:

#include<stdio.h>
main( ) {
int a, b, c, d;
a = 20; b = 15;

c = ++a - b;
printf (" a = %d b = %d and c = %d\n", a, b, c);

d = b++ +a;
printf ("a = %d b = %d and d = %d\n", a, b, d);

printf ("a/b = %d \n", a/b);


printf ("a%b = %d \n", a%b);
printf ("a*=b= %d \n", a*=b);
printf ("%d \n", (c>d)? 1:0);
printf ("%d \n", (c<d)? 1 : 0);
}

Ans:

a = 21 b = 15 and c = 6
a = 21 b = 16 and d = 36
a/b=1
a%b=5
a*= b = 336
0
1

Dr. Abu Nowshed Chy 6


Lecturer, Dept. of CSE, CU
10. Describe the outputs generated by the following programs:

#include<stdio.h>
main( ) {
char choice = 'W';
switch(choice)
{
case 'R':
printf("RED");
case 'W':
printf("WHITE");
case 'B':
printf("BLUE");
}
}
Ans:
WHITEBLUE.

When the switch statement is executed, it found the value of the expression is ‘W’. Then the
value compared against the values ‘R’, ‘W, ‘B’. Here a case is found (case ‘W’: ) whose value
matches with value of the expression ( ‘W’ ) so the control is transferred directly to that case.
Next the block of statements that follows the ( case ‘W’: ) are executed.
As no break statements is found at the end of that block which signals the end of a particular case
block and causes an exit from the switch statement, the subsequent case ( case ‘B’: ) is also
executed.

Hence the output is: WHITEBLUE

11. Describe the outputs generated by the following programs:

#include<stdio.h>
main( ) {
int a, b, x=0;
for (a=0; a<5; ++a)
for (b=0; b<a; ++b)
{
x += (a+b-1);
printf ("%d ", x);
}
printf ("\n x=%d", x);
}
Ans:
Here the output is:

0 1 3 5 8 12 15 19 24 30
x=30

Dr. Abu Nowshed Chy 7


Lecturer, Dept. of CSE, CU

You might also like