C Program to Check Prime Number Using a Function

C Program to Check Prime Number Using a Function

A student claims that any odd number is prime. A divisor test can show why odd composites such as nine still fail.

A prime integer is at least two and has no positive divisors other than one and itself. A factor test only needs candidates through its square-root boundary.

Before this exercise, review loops and stopping conditions and functions and parameters. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why prime function uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Use one as the candidate and explain why it is not prime.

Prime Function: follow the data

  1. Given data

    A student claims that any odd number is prime. A divisor test can show why odd composites such as nine still fail.

  2. Rule

    A prime integer is at least two and has no positive divisors other than one and itself. A factor test only needs candidates through its square-root boundary.

  3. Observed result

    49: not prime

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

If a composite has factors a and b, at least one is no greater than its square root. The condition divisor <= value / divisor reaches that boundary without computing divisor * divisor, which could overflow for large values.

Return false immediately when a divisor leaves remainder zero. Return true only after every required divisor was tried. A named helper makes the input-to-result contract explicit.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
static int is_prime(int value) {
  if (value < 2) return 0;
  for (int divisor = 2; divisor <= value / divisor; divisor++) {
    if (value % divisor == 0) return 0;
  }
  return 1;
}
int main(void) {
  int number = 49;
  printf("%d: %s\n", number, is_prime(number) ? "prime" : "not prime");
  return 0;
}

Expected output

49: not prime
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
static int is_prime(int value) {
  if (value < 2) return 0;
  for (int divisor = 2; divisor <= value / divisor; divisor++) {
    if (value % divisor == 0) return 0;
  }
  return 1;
}
int main(void) {
  int number = 49;
  printf("%d: %s\n", number, is_prime(number) ? "prime" : "not prime");
  return 0;
}

The candidate is 49; the helper first rejects values less than two.

Each tested divisor is a possible witness of compositeness. A successful remainder-zero test stops that candidate’s search; finding no witness within the bound confirms it.

Follow the worked calculation

  1. Start with the sample

    The candidate is 49; the helper first rejects values less than two.

  2. Apply the rule

    Each tested divisor is a possible witness of compositeness. A successful remainder-zero test stops that candidate’s search; finding no witness within the bound confirms it.

  3. Check the result

    49: not prime

    49: not prime
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
static int is_prime(int value) {
  if (value < 2) return 0;
  for (int divisor = 2; divisor <= value / divisor; divisor++) {
    if (value % divisor == 0) return 0;
  }
  return 1;
}
int main(void) {
  int number = 49;
  printf("%d: %s\n", number, is_prime(number) ? "prime" : "not prime");
  return 0;
}

Use one as the candidate and explain why it is not prime.

Worked answer

The first guard rejects one before entering the divisor loop. A loop with no iterations is not sufficient by itself to establish primality.

C17 · complete program

#include <stdio.h>
static int is_prime(int value) {
  if (value < 2) return 0;
  for (int divisor = 2; divisor <= value / divisor; divisor++) {
    if (value % divisor == 0) return 0;
  }
  return 1;
}
int main(void) {
  int number = 1;
  printf("%d: %s\n", number, is_prime(number) ? "prime" : "not prime");
  return 0;
}

Expected output

1: not prime
Run in C compiler →

Recognize the important boundaries

Zero, one and negative integers are not primes. Equality at the divisor boundary matters: a square such as 49 must test divisor seven.

This trial-division helper is useful for small classroom values; listing very large intervals efficiently needs a more suitable algorithm, such as a sieve.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Why test the boundary divisor when number is a perfect square?
Choose one answer. You can try again.

Answer and explanation

Its square-root divisor can be the factor that proves it composite.

Correct. Skipping equality would miss values such as 49.

Use the idea independently

A prime integer is at least two and has no positive divisors other than one and itself. A factor test only needs candidates through its square-root boundary.

Continue with function contracts.

Continue with all factors.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Print Primes in a Range Using Functions →