0% found this document useful (0 votes)
17 views7 pages

C Program Output Analysis

The document contains 10 programming questions and their solutions. Each question provides the code for a short program and asks the user to determine the output. The responses provide detailed explanations for the output of each program by walking through the code line-by-line.

Uploaded by

Hozaifa Hamim
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

C Program Output Analysis

The document contains 10 programming questions and their solutions. Each question provides the code for a short program and asks the user to determine the output. The responses provide detailed explanations for the output of each program by walking through the code line-by-line.

Uploaded by

Hozaifa Hamim
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

Common questions

Powered by AI

Operator precedence dictates that pre-increment (++a) is performed before subtraction in c = ++a - b, while b++ is evaluated post-assignment in d = b++ + a. Understanding precedence is essential to predict how these operations modify 'a', 'b', 'c', and 'd', shown in a=21, b=16, c=6, and d=36 .

The nested loops create an accumulation that breaks once a defined sum boundary (sum < 100) is exceeded. Flow control here, with 'for' and 'if' statements, dynamically adjusts 'sum' with each increment of 'i'. It demonstrates conditional exits and control in loop constructs, culminating in the sum sequence stopping at 105 after reaching 91 .

In the switch statement, when choice 'W' matches, it executes the corresponding case and every subsequent case until the end, unless a break statement is encountered. Without breaks, it executes both 'W' and 'B', resulting in the output 'WHITEBLUE' .

The 'printf' function displays the integer values 32768 and 32777 as they are in the first call. In the second call, the integer value of 'T' (84 in ASCII) is printed. In the final call, it prints the character corresponding to ASCII value 97, which is 'a' .

The modulo operation a%b yields the remainder of the division of a by b, resulting in 10%11 which equals 10. The conditional operator (c<d) checks if c is less than d; returns c if true, otherwise d. Since c equals 11 and d equals 31, the conditional operator returns c in this case, displaying 11. The floating-point division of a/b casts the result to 1.000000 .

In the program, the pre-increment (++x) and post-decrement (y--) operators affect the values of m, n, x, and y. Line 4 increments x to 11 before assigning to m, and decrements y post assigning its value to n, hence n retains the value 9 before decrementing y to 8. On line 5, m's post-decrement and x's pre-increment also affect the output, resulting in m=12, n=9, x=13, and y=8 .

Integer division 'a/b' discards any decimal, resulting in 1 because 21 divided by 16 equals 1 with remainder 5, calculated by 'a%b'. These operations emphasize how C handles division and modulus, influencing branching and assignment in subsequent operations, crucial for expressions and logic in C .

Nested loops gradually update variable 'x' by iterating with 'a' and 'b'. As 'b' varies from 0 to less than 'a', 'x' accumulates the calculated value (a+b-1). After full execution, values of 0, 1, 3, 5, etc., lead to final x=30. With each complete iteration of b across a values, 'x' integrates these stepwise calculations .

In the given while loop with 'i--', 'x' is incremented and 'y' is decremented. Each iteration adjusts values, producing sequential outputs (X=1 Y=4 and X=2 Y=3). Changing the decrement pace would alter these values, demonstrating the importance of precise loop control for expected outcomes .

The 'do-while' loop checks the condition after executing the loop body. The loop executes the body at least once, incrementing i and updating x when i%5==0. For i from 0 to 19, x increments at the specific places (5, 10, 15), resulting in the output '1234'. This occurs because the post-increment in the loop ensures execution before checking i<20 .

You might also like