---
title: "Correct program: do-while and pow"
subject: "Computer Programming"
chapter: 3
key: "semester_1/computer_programming:correct-program-sum-root"
source: https://www.bctengineer.com/q/semester_1/computer_programming/correct-program-sum-root
site: BCT Engineering
---

# Correct program: do-while and pow

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

- Asked in: 2070 Asar, 3+2 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/correct-program-sum-root
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-3

## Question

Rewrite the following program by correcting any syntactical errors, if present. Also show the output of the corrected code.
```
#include <stdio.h>
int main ( )
{
  float root, int i = 1;
  do { sum = 2i - 1;
    print ("\t%d\n", sum);
    i *= 5/3;
  } while (sum <= 15)
  root = pow (i, 1/2);
  print ("\n%.3f", root);
  return void;
}
```

## Short answer

After correcting syntax errors (missing ;, print to printf, return void to return 0, adding math.h), the program runs an infinite loop printing 1 endlessly because integer division 5/3 is 1, so i stays 1 and sum is always 1; the final root line is never reached.

## Answer

#### Errors found
- `float root, int i = 1;` mixes two types in one declaration; write `float root; int i = 1, sum;`.
- `sum` is not declared.
- `2i - 1` is invalid; write `2*i - 1`.
- `print` should be `printf` (twice).
- A `;` is missing after `while (sum <= 15)`.
- `return void;` should be `return 0;`.
- `pow` needs `#include <math.h>`.
- (Logic, not syntax) `1/2` is integer division and equals 0, so `pow(i, 1/2)` is `pow(i, 0)` = 1; write `1.0/2` (or use `sqrt`).

#### Corrected code (syntax errors only)
```c
#include <stdio.h>
#include <math.h>
int main(void)
{
    float root;
    int i = 1, sum;
    do {
        sum = 2 * i - 1;
        printf("\t%d\n", sum);
        i *= 5 / 3;
    } while (sum <= 15);
    root = pow(i, 1 / 2);
    printf("\n%.3f", root);
    return 0;
}
```

#### Output
Because `5/3` is integer division, it equals 1, so `i *= 5/3` leaves `i` at 1 forever. Then `sum` is always 1, `sum <= 15` is always true, and the program never ends. It prints the following endlessly:
```text
	1
	1
	1
	1
	...
```
The final `root` line is never reached.

#### What the program intends
If the update is meant to move `i` forward (for example `i += 1`), the loop prints the odd numbers 1, 3, 5, ..., 17 (it stops when `sum` reaches 17, which is greater than 15), then `i` is 10 and `root = sqrt(10)`:
```c
#include <stdio.h>
#include <math.h>
int main(void)
{
    float root;
    int i = 1, sum;
    do {
        sum = 2 * i - 1;
        printf("\t%d\n", sum);
        i += 1;
    } while (sum <= 15);
    root = pow(i, 1.0 / 2);
    printf("\n%.3f", root);
    return 0;
}
```
```text
	1
	3
	5
	7
	9
	11
	13
	15
	17

3.162
```

## Check yourself

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

1. Correct declaration is
   A. float root; int i = 1, sum;
   B. float root, int i = 1;
   C. float root, i;
   D. int float root;
   Answer: A. Separate types. (Key point: Declaration)

2. i *= 5/3 leaves i at 1 because
   A. 5/3 is 0
   B. 5/3 is integer division giving 1
   C. i is float
   D. pow changes i
   Answer: B. i*1 = i. (Key point: Infinite loop)

3. pow(i, 1/2) is wrong because
   A. 1/2 is 2
   B. pow takes one argument
   C. 1/2 is 0 in integer division
   D. i is negative
   Answer: C. Use 1.0/2. (Key point: pow exponent)

4. With i += 1 the loop stops after printing
   A. 15
   B. 16
   C. 19
   D. 17
   Answer: D. sum 17 > 15 ends. (Key point: Intended output)

## Related in Computer Programming

- [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
- [Correct program: character arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-char-arrays) — Computer Programming, chapter 7
- [Output of arithmetic expression program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aoutput-expression-xyz) — Computer Programming, chapter 3
- [Print float to 2 and 4 decimals](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aprint-float-two-four-decimals) — Computer Programming, chapter 4
- [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

- [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
- [Trellis decoding of 1/2 rate conv. code (3-bit errors)](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Aconv-trellis-3bit) — Data Communication, chapter 8, asked 2×
- [Trellis decoding of 1/2 rate conv. code (2-bit errors)](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Aconv-trellis-2bit) — Data Communication, chapter 8, asked 2×
