---
title: "Count even and odd until -1"
subject: "Computer Programming"
chapter: 5
key: "semester_1/computer_programming:count-even-odd-until-minus1"
source: https://www.bctengineer.com/q/semester_1/computer_programming/count-even-odd-until-minus1
site: BCT Engineering
---

# Count even and odd until -1

Computer Programming · Chapter 5: Control statements

- Asked in: 2070 Asar, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/count-even-odd-until-minus1
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-5

## Question

Write a program to read the number until -1 is encountered. Also count the number of even number and odd numbers entered by the user.

## Short answer

Read numbers until -1; for each non-negative entry, increment even counter if n%2==0 else odd counter. -1 stops and is not counted. Print totals.

## Answer

Numbers are read until -1 is entered (the -1 is not counted). A number is even if `n % 2 == 0`, otherwise it is odd.

```c
#include <stdio.h>
int main(void)
{
    int n, even = 0, odd = 0;
    printf("Enter numbers (-1 to stop):\n");
    scanf("%d", &n);
    while (n != -1)
    {
        if (n % 2 == 0)
            even++;
        else
            odd++;
        scanf("%d", &n);
    }
    printf("Even numbers = %d\nOdd numbers = %d\n", even, odd);
    return 0;
}
```
Output for the input 4, 7, 10, 3, 8, 9, -1:
```text
Enter numbers (-1 to stop):
Even numbers = 3
Odd numbers = 3
```

Even numbers are 4, 10, 8 (3 of them); odd numbers are 7, 3, 9 (3 of them).

## Check yourself

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

1. How is an even number detected?
   A. n / 2 == 0
   B. n % 2 == 0
   C. n % 2 == 1
   D. n % 10 == 0
   Answer: B. Remainder 0 on dividing by 2. (Key point: Even test)

2. What is done with -1?
   A. Counted as odd
   B. Counted as even
   C. Stops input, not counted
   D. Added to sum
   Answer: C. -1 is only a stop signal. (Key point: Sentinel)

3. Why scanf at end of loop body?
   A. To print
   B. To reset
   C. To count
   D. To read next number before re-testing
   Answer: D. Loop condition needs a fresh value. (Key point: Second scanf)

4. Input 4,7,10,3,8,9,-1 gives?
   A. 3 even, 3 odd
   B. 4 even, 2 odd
   C. 2 even, 4 odd
   D. 3 even, 4 odd
   Answer: A. Even: 4,10,8; odd: 7,3,9. (Key point: Counts)

## Related in Computer Programming

- [Odd and even numbers to separate files](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-odd-even-until-no) — Computer Programming, chapter 10, asked 2×
- [Count prime and composite until -1](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acount-prime-composite-until-minus1) — Computer Programming, chapter 5
- [Sum and average until 0 is entered](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-average-until-zero) — Computer Programming, chapter 5
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [Sum of digits until single digit](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adigit-sum-single-digit) — Computer Programming, chapter 5, asked 2×
- [Algorithm for numbers divisible by 9 not 6](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgo-flowchart-divisible-9-not-6) — Computer Programming, chapter 2

## Related in other subjects

- [Handshaking theorem; even number of odd vertices](https://www.bctengineer.com/question.md?key=semester_4%2Fdiscrete_structure%3Ahandshaking-even-odd-degree-proof) — Discrete Structure, chapter 4, asked 3×
- [DFA: even a and odd b](https://www.bctengineer.com/question.md?key=semester_3%2Ftheory_of_computation%3Adfa-even-a-odd-b) — Theory of Computation, chapter 2
- [RE: even a's followed by odd b's](https://www.bctengineer.com/question.md?key=semester_3%2Ftheory_of_computation%3Are-even-a-odd-b) — Theory of Computation, chapter 2
- [8086 ALP: sum of 100 even numbers](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-sum-even-numbers) — Microprocessor, chapter 3
