Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C Program to Check Prime Number

Posted in C Tutorial
Updated on Oct 27, 2024
By Mari Selvan
πŸ‘οΈ 248 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
C Program to Check Prime Number

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the landscape of programming, dealing with prime numbers is a fundamental and interesting task. Prime numbers, those divisible only by 1 and themselves, play a crucial role in various mathematical and computational applications.

In this tutorial, we will explore a simple yet effective C program to check whether a given number is a prime number.

πŸ“„ Example

Let's delve into the C code that performs the prime number check.

isPrime.c
Copied
Copy To Clipboard
#include <stdio.h>
#include <stdbool.h>

// Function to check if a number is prime
bool isPrime(int number) {
  // 0 and 1 are not prime numbers
  if (number <= 1) {
    return false;
  }

  // Check for factors from 2 to the square root of the number
  for (int i = 2; i * i <= number; ++i) {
    if (number % i == 0) {
      return false; // Found a factor, not a prime number
    }
  }

  return true; // No factors found, it's a prime number
}

// Driver program
int main() {
  // Replace this value with your desired number
  int testNumber = 17;

  // Call the function to check if the number is prime
  if (isPrime(testNumber)) {
    printf("%d is a prime number.\n", testNumber);
  } else {
    printf("%d is not a prime number.\n", testNumber);
  }

  return 0;
}

πŸ’» Testing the Program

To test the program with a different number, replace the value of testNumber in the main function.

Output
17 is a prime number.

Compile and run the program to see whether the number is a prime number.

🧠 How the Program Works

  1. The program defines a function isPrime that takes an integer as input and returns a boolean indicating whether the number is prime.
  2. Inside the function, it checks if the number is less than or equal to 1; if so, it's not prime.
  3. It then iterates through potential factors from 2 to the square root of the number.
  4. If it finds any factor, the number is not prime; otherwise, it is prime.
  5. The main function tests the program with a specific number (in this case, 17).

πŸ“ Between the Given Range

Let's dive into the C code that checks and lists prime numbers in the specified range.

isPrime.c
Copied
Copy To Clipboard
#include <stdio.h>

// Function to check if a number is prime
int isPrime(int num) {
    if (num <= 1) {
        return 0; // Not prime
    }

    for (int i = 2; i * i <= num; ++i) {
        if (num % i == 0) {
            return 0; // Not prime
        }
    }

    return 1; // Prime
}

// Function to list prime numbers in a range
void listPrimesInRange(int start, int end) {
    printf("Prime Numbers in the Range %d to %d:\n", start, end);

    for (int i = start; i <= end; ++i) {
        if (isPrime(i)) {
            printf("%d ", i);
        }
    }

    printf("\n");
}

// Driver program
int main() {
    // Range from 1 to 20
    int start = 1;
    int end = 20;

    // Call the function to list prime numbers in the range
    listPrimesInRange(start, end);

    return 0;
}

πŸ’» Testing the Program

The program is set to check and list prime numbers in the range from 1 to 20. No additional input is required.

Output
Prime Numbers in the Range 1 to 20:
2 3 5 7 11 13 17 19

Compile and run the program to see the prime numbers in the specified range.

🧠 How the Program Works

  1. The program defines a function isPrime that checks if a given number is prime.
  2. Another function listPrimesInRange lists prime numbers in the specified range.
  3. The main function calls listPrimesInRange with the range from 1 to 20.

🧐 Understanding the Concept of Prime Numbers

Before diving into the code, let's briefly understand prime numbers.

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. For example, 2, 3, 5, 7, 11, and 13 are prime numbers.

🎒 Optimizing the Program

While the provided program is effective, you can explore optimizations such as using the square root of the number as the loop limit for improved efficiency.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (C Program to Check Prime Number) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy