Open links in new tab
    • Work Report
    • Email
    • Rewrite
    • Speech
    • Title Generator
    • Smart Reply
    • Poem
    • Essay
    • Joke
    • Instagram Post
    • X Post
    • Facebook Post
    • Story
    • Cover Letter
    • Resume
    • Job Description
    • Recommendation Letter
    • Resignation Letter
    • Invitation Letter
    • Greeting Message
    • Try more templates
  1. A prime number is a natural number greater than 1 that is divisible only by 1 and itself. Below is a C program to determine whether a given number is prime or not.

    #include <stdio.h>

    int main() {
    int n, i, flag = 0;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    // 0 and 1 are not prime numbers
    if (n == 0 || n == 1) {
    flag = 1;
    }

    // Check divisors from 2 to n/2
    for (i = 2; i <= n / 2; ++i) {
    if (n % i == 0) {
    flag = 1; // Not a prime number
    break;
    }
    }

    if (flag == 0)
    printf("%d is a prime number.\n", n);
    else
    printf("%d is not a prime number.\n", n);

    return 0;
    }
    Copied!

    Optimized Approach

    To improve efficiency, you can reduce the range of divisors to the square root of the number (√n), as no factor greater than √n exists for any composite number.

    Like
    Dislike
    1. Prime Number Program in C - GeeksforGeeks

      Jul 11, 2025 · A prime number is a natural number greater than 1 and is completely divisible only by 1 and itself. In this article, we will learn how to check whether the given number is a prime number or …

    2. C Program to Check Whether a Number is Prime or Not

      In this example, you will learn to check whether an integer entered by the user is a prime number or not with explanation...

    3. C Program to Find Prime Number (6 Ways With Code)

      A prime number is greater than 1 and divisible only by 1 and itself. In this tutorial, you’ll learn how to write a C program for prime numbers to check whether a given number is prime or not.

    4. Prime Number Program In C - Online Tutorials Library

      Any whole number which is greater than 1 and has only two factors that is 1 and the number itself, is called a prime number. Other than these two number it has no positive divisor.

    5. C Program to Check Prime Number: Simple Methods …

      May 9, 2025 · Learn how to check prime numbers in C with simple methods. This guide offers beginner-friendly techniques to help you efficiently identify prime numbers in C.

    6. Prime Number Program in C: A Step-by-Step Guide - NxtWave

      Learn to create a prime number program in C with examples. Explore prime no. code in C using loops, recursion, functions, and sqrt (N) logic to identify primes efficiently.

    7. Prime Number Program in C

      May 28, 2024 · In this example I have explained how to write a Prime Number Program in C. I have used very simple conditional cases and there is not a single loop code.

    8. Prime Number Program In C | 12 Methods Explained …

      In this article, we will discuss and understand different ways to write and run a prime number program in C language. We will start by understanding the concept of prime numbers first and then explore a fundamental algorithm.

    9. Prime Numbers in C - C# Corner

      In this blog, I am going to explain how to check a number for whether it is a prime number or not.

    10. Prime Number Program in C – All Methods Explained

      Learn how to write a Prime Number Program in C using loops, recursion, and functions. Includes step-by-step examples and code for beginners.

  2. People also ask