---
title: "Output of arithmetic expression program"
subject: "Computer Programming"
chapter: 3
key: "semester_1/computer_programming:output-expression-xyz"
source: https://www.bctengineer.com/q/semester_1/computer_programming/output-expression-xyz
site: BCT Engineering
---

# Output of arithmetic expression program

Computer Programming · Chapter 3: Introduction to ‘C’ programming

- Asked in: 2069 Chaitra, 3 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/output-expression-xyz
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-3

## Question

Give the output of the following program and justify your answer with reason.
```
#include<stdio.h>
int main()
{
  int x=3,y=5,z=7;
  int a,b;
  a=x*2+y/5-z*y;
  b=++x*(y-3)/2-z++*y;
  printf("a=%d",a);
  printf("b=%d",b);
  return 0;
}
```

## Short answer

The output is a=-28b=-31 on one line. a = 3*2 + 5/5 - 7*5 = -28. For b, ++x makes x=4, then 4*(5-3)/2 =4, z++*y uses z=7, so 7*5=35, b=4-35=-31.

## Answer

#### Output
```text
a=-28b=-31
```

(The two `printf` calls have no newline, so both values appear on one line.)

#### Reason
Initially x = 3, y = 5, z = 7.

**For a:** `a = x*2 + y/5 - z*y`
- `x*2` = 6, `y/5` = 1, `z*y` = 35 (multiplication and division are done before + and -)
- a = 6 + 1 - 35 = **-28**

**For b:** `b = ++x*(y-3)/2 - z++*y`
- `++x` makes x = 4 first (pre-increment), then `4 * (5-3) / 2` = 4 * 2 / 2 = 4 (left to right)
- `z++*y` uses the old z = 7: 7 * 5 = 35, and then z becomes 8
- b = 4 - 35 = **-31**

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. With x=3, y=5, z=7, what is a = x*2 + y/5 - z*y?
   A. -28
   B. -29
   C. 6
   D. -27
   Answer: A. 6 + 1 - 35 = -28; multiplication and division come before + and -. (Key point: Value of a)

2. In b = ++x*(y-3)/2 - z++*y, what value of x is used in the first term?
   A. 3, the old value
   B. 4, since ++x increments first
   C. 5
   D. It is undefined
   Answer: B. Pre-increment raises x to 4 before it is used, so the term is 4*2/2 = 4. (Key point: Pre-increment)

3. In z++*y, which value of z is used in the multiplication?
   A. 8, the incremented value
   B. 7, and z becomes 8 afterwards
   C. 5
   D. 6
   Answer: B. Post-increment uses the old value 7, giving 7*5 = 35, then z becomes 8. (Key point: Post-increment)

4. What does the program print?
   A. a=-28 b=-31 on separate lines
   B. a=-28b=-31 on one line
   C. a=-28b=-35
   D. a=-29b=-31
   Answer: B. There is no newline in the printf calls, so the output is a=-28b=-31 (b = 4 - 35 = -31). (Key point: Printed output)

## Related in Computer Programming

- [Output of printf statements](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aprintf-output-a-b) — Computer Programming, chapter 4
- [Correct program: character arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-char-arrays) — Computer Programming, chapter 7
- [Correct program: do-while and pow](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-sum-root) — Computer Programming, chapter 3
- [Correct program: Celsius to Fahrenheit](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-celsius-fahrenheit) — Computer Programming, chapter 3
- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3
- [Need of stdio.h header file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astdio-header-need) — Computer Programming, chapter 3

## Related in other subjects

- [Pyramid pattern output program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apyramid-program) — Object Oriented Programming, chapter 8
- [Cube of int, float, double by overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acube-overloaded-program) — Object Oriented Programming, chapter 3
- [Return current path in high-speed circuits](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Ahigh-speed-ground-return) — Instrumentation II, chapter 6
- [Regular expression and FSA: at least three b's](https://www.bctengineer.com/question.md?key=semester_4%2Fdiscrete_structure%3Afsa-at-least-three-b) — Discrete Structure, chapter 2
