THE SYLLABUS QUESTION
What you need to solve
Write a program that finds if a given number is a prime number.
One integer between -1,000,000,000 and 1,000,000,000.
UNDERSTAND THE IDEA
Explanation
A prime number is an integer greater than 1 with exactly two positive divisors: 1 and itself. Numbers below 2 are not prime.
If a composite number has a divisor, at least one divisor is no greater than its square root. The condition divisor <= number / divisor avoids overflow from divisor * divisor.
PLAN BEFORE CODING
Algorithm
- Read the integer and validate its range.
- Treat numbers less than 2 as not prime.
- Test divisors from 2 while divisor <= number / divisor.
- Stop if a divisor divides the number exactly.
- Print the primality result.
SEE THE CONTROL FLOW
Flowchart
Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.
On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗
#include <stdio.h>
int main(void) {
int number, prime;
if (scanf("%d", &number) != 1 || number < -1000000000 || number > 1000000000) {
puts("Invalid input.");
return 1;
}
prime = number >= 2;
for (int divisor = 2; prime && divisor <= number / divisor; ++divisor) {
if (number % divisor == 0) prime = 0;
}
printf("%d is %sprime.\n", number, prime ? "" : "not ");
return 0;
}
Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.
Compile and run locally
gcc -std=c17 prime-number.c -o lab
./labOn Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.
FOLLOW THE VALUES
Dry run
| Step / state | Operation | Result |
|---|---|---|
| Input 29 | Try divisor 2 | Remainder 1 |
| Try divisors 3, 4, 5 | No zero remainder | Still prime candidate |
| Next divisor 6 | 6 > 29 / 6 | Stop: 29 is prime |
CHECK THE BEHAVIOR
Sample input & output
Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.
Sample 1
29
29 is prime.
Sample 2
1
1 is not prime.
Sample 3
49
49 is not prime.
Common mistakes
- One and zero are not prime.
- Use a safe loop bound instead of squaring a potentially large divisor.
WHY THIS GROWTH RATE?
Time and space complexity
Time O(√n) for n >= 2; auxiliary space O(1).
Let n be the value being tested. If n has a factor larger than √n, its partner factor must be smaller than √n. Therefore the loop only needs to test divisors up to √n, using divisor <= n / divisor to avoid multiplying two divisors.
For a prime input, no early exit occurs and roughly √n divisors are tested. Each remainder test has constant cost for fixed-width integers, giving worst-case O(√n) time. Inputs below 2, or an early factor such as 2, finish in O(1). Only a few counters and flags are stored: O(1) auxiliary space.
Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.