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

# Count prime and composite until -1

Computer Programming · Chapter 5: Control statements

- Asked in: 2069 Chaitra, 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-prime-composite-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 prime number and composite numbers entered by the user.

## Short answer

Read numbers until -1. For each n>1, test primality by checking divisors up to sqrt(n); increment prime or composite accordingly. Ignore 0,1.

## Answer

Read numbers until -1. A prime number is greater than 1 and has no divisor other than 1 and itself; a composite number is greater than 1 and has other divisors. To test a number, check divisibility by 2, 3, ... up to √n (`i * i <= n`). The numbers 0 and 1 are neither prime nor composite, so they are not counted.

```c
#include <stdio.h>
int main(void)
{
    int n, i, isprime, prime = 0, comp = 0;
    printf("Enter numbers (-1 to stop):\n");
    scanf("%d", &n);
    while (n != -1)
    {
        if (n > 1)
        {
            isprime = 1;
            for (i = 2; i * i <= n; i++)
                if (n % i == 0)
                {
                    isprime = 0;
                    break;
                }
            if (isprime) prime++;
            else comp++;
        }
        scanf("%d", &n);
    }
    printf("Prime numbers = %d\nComposite numbers = %d\n", prime, comp);
    return 0;
}
```
Output for the input 2, 9, 13, 1, 20, 7, -1:
```text
Enter numbers (-1 to stop):
Prime numbers = 3
Composite numbers = 2
```

Primes: 2, 13, 7. Composite: 9, 20. The number 1 is neither.

## Check yourself

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

1. Up to what divisor is checked?
   A. n
   B. sqrt(n) (i*i <= n)
   C. n/2 only
   D. n-1 only
   Answer: B. A factor pair has one factor <= sqrt(n). (Key point: Prime test limit)

2. How are 0 and 1 treated?
   A. Prime
   B. Composite
   C. Neither prime nor composite, not counted
   D. Prime if 1
   Answer: C. Only n > 1 counted. (Key point: 0 and 1)

3. What is break for in the for loop?
   A. Ends program
   B. Reads next
   C. Counts
   D. Stops once a divisor is found
   Answer: D. A divisor shows non-prime. (Key point: Break)

4. Input 2,9,13,1,20,7,-1 gives?
   A. 3 prime, 2 composite
   B. 3 prime, 3 composite
   C. 4 prime, 2 composite
   D. 2 prime, 3 composite
   Answer: A. Primes 2,13,7; composite 9,20. (Key point: Counts)

## Related in Computer Programming

- [Count even and odd until -1](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acount-even-odd-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×
- [nth Fibonacci term prime check](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afibonacci-nth-prime) — Computer Programming, chapter 6
- [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×

## Related in other subjects

- [Prime number program: procedural and OOP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprime-procedural-oop) — Object Oriented Programming, chapter 1
- [Count number of 1s in ROM data](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-count-ones-asm) — Embedded System, chapter 9, asked 2×
- [Count number of 0s in ROM data](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-count-zeros-asm) — Embedded System, chapter 9, asked 2×
- [Prime number checking processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-prime-check) — Embedded System, chapter 2
