🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-program-to-check-whether-a-number-is-prime-or-not
Prime Number Program in C - GeeksforGeeks
Input: n = 15 Output: 15 is NOT prime Explanation: 15 has divisors other than 1 and 15 (i.e., 3 and 5). Hence, it is not a prime number. We can check whether a number is prime using various approaches:
Published   July 11, 2025
🌐
Programiz
programiz.com › c-programming › examples › prime-number
C Program to Check Whether a 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 // change flag to 1 for non-prime number if (n == 0 || n == 1) flag = 1; for (i = 2; i <= n / 2; ++i) { // if n is divisible by i, then n is not prime ...
People also ask

How do you check whether a given number is prime or not?
If a number is divisible by itself or by 1, then it is said to be a prime number. We can use c program to find if a number is prime or not.
🌐
pwskills.com
pwskills.com › blog › cpp › what is the program of c?
C Program To Check Whether A Number Is Prime Or Not
What are different ways of checking whether a number is prime or not?
We can use loops, functions, recursions and other methods to check whether a given number is prime or not.
🌐
pwskills.com
pwskills.com › blog › cpp › what is the program of c?
C Program To Check Whether A Number Is Prime Or Not
How do I find prime numbers from 1 to 100 using the C program?
We can use the Sieve of Eratosthenes to print prime numbers in a given range from a given prime number to a given value n.
🌐
pwskills.com
pwskills.com › blog › cpp › what is the program of c?
C Program To Check Whether A Number Is Prime Or Not
🌐
Vultr
docs.vultr.com › clang › examples › check-whether-a-number-is-prime-or-not
C Program to Check Whether a Number is Prime or Not | Vultr Docs
November 7, 2024 - This code defines a function isPrime(), which takes an integer and returns a boolean indicating whether the number is prime.
🌐
PW Skills
pwskills.com › blog › cpp › what is the program of c?
C Program To Check Whether A Number Is Prime Or Not
March 2, 2026 - Prime check runs in O(√n) time. The check i * i <= num is used instead of i <= num for better performance. First filter is num <= 1 which handles invalid/edge input early. Check the algorithm to write a C program for checking whether a prime ...
🌐
BeginnersBook
beginnersbook.com › 2022 › 07 › c-program-to-check-whether-a-number-is-prime-or-not
C Program to Check Whether a Number is Prime or Not
#include <stdio.h> int main() { //Here flag value 0 means prime number and //1 means non-prime number int num, i, flag = 0; printf("Enter a number to check whether prime or not: "); scanf("%d", &num); // 0 and 1 are not the prime numbers so setting // the flag to 1 if entered number is 0 or ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-check-whether-a-number-is-prime-or-not
Check Prime Number in Python - GeeksforGeeks
from math import sqrt def prime(n, i): if i == 1: return True if n % i == 0: return False return prime(n, i - 1) n = 13 print(prime(n, int(sqrt(n)))) ... Function checks whether n is divisible by i.
Published   May 16, 2026
🌐
PREP INSTA
prepinsta.com › home › c program › c program to check whether a number is prime number or not
Prime Number Program in C | PrepInsta
May 31, 2025 - #include <stdio.h> int checkPrime(int n, int i) { // we check if i is divisor of n or not here // 0, 1 & neg. nos are not prime if (n < 2) return 0; // if this satisfies then its prime as we // have completed recursion from 2 to n if (i == n) ...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › c-program-to-check-whether-a-number-is-prime-or-not
Prime Number Program In C
November 7, 2023 - #include <stdio.h> int main() { ... % loop) == 0) { prime = 0; } } if (prime == 1) printf("%d is prime number.", number); else printf("%d is not a prime number.", number); return 0; }...
🌐
Simplilearn
simplilearn.com › home › resources › software development › prime number program in c: logic, code, and examples
C Program to Check Prime Number: Simple Methods Explained
3 weeks ago - 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.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Codeforwin
codeforwin.org › home › c program to check whether a number is prime number or not
C program to check whether a number is prime number or not - Codeforwin
July 20, 2025 - Outside the loop check the current value of isPrime. According to our assumption if it is equal to 1 then the number is prime otherwise composite. /** * C program to whether a number is prime number or not */ #include <stdio.h> int main() { int i, num, isPrime; /* * isPrime is used as flag variable.
🌐
Quora
quora.com › How-can-I-write-a-C-program-to-check-whether-a-number-is-prime-or-composite-using-for-loop
How to write a C program to check whether a number is prime or composite using for loop - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
Quora
quora.com › How-would-one-write-a-program-to-check-whether-a-number-is-a-prime-number-or-not-using-a-function
How would one write a program to check whether a number is a prime number or not using a function? - Quora
Answer (1 of 3): As said already, each algorithm to solve the problem can be set into a subroutine or function, which submits back a code for “prime/not prime”, e.g. logical TRUE/FALSE or as integer the smallest divisor or an 1 or so. The algorithm given by Mr.
🌐
CodingBroz
codingbroz.com › home › c program to check whether a number is prime or not
C Program To Check Whether a Number is Prime or Not - CodingBroz
November 23, 2021 - Note: 1 is neither a prime number nor composite number. So, without further ado, let’s begin the tutorial. ... // C Program To Check Whether a Number is Prime or Not #include <stdio.h> int main(){ int num, i, c = 0; // Asking for Input printf("Enter an Number: "); scanf("%d", &num); // logic for (i = 1; i <= num; i++){ if (num % i == 0){ c++; } } if (c == 2){ printf("%d is a Prime Number.", num); } else { printf("%d is not a Prime Number.", num); } return 0; }
Top answer
1 of 2
1
Pythondef is_prime(num):    if num <= 1:        return False    for i in range(2, int(num ** 0.5) + 1):        if num % i == 0:            return False    return Truenum = int(input("Enter a number: "))if is_prime(num):    print(f"{num} is a prime number.")else:    print(f"{num} is not a prime number.")Javaimport java.util.Scanner;public class Main {    public static boolean isPrime(int num) {        if (num <= 1) {            return false;        }        for (int i = 2; i <= Math.sqrt(num); i++) {            if (num % i == 0) {                return false;            }        }        return true;    }    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        System.out.print("Enter a number: ");        int num = scanner.nextInt();        if (isPrime(num)) {            System.out.println(num + " is a prime number.");        } else {            System.out.println(num + " is not a prime number.");        }    }}C++#include #include using namespace std;bool isPrime(int num) {    if (num <= 1) {        return false;    }    for (int i = 2; i <= sqrt(num); i++) {        if (num % i == 0) {            return false;        }    }    return true;}int main() {    int num;    cout << "Enter a number: ";    cin >> num;    if (isPrime(num)) {        cout << num << " is a prime number." << endl;    } else {        cout << num << " is not a prime number." << endl;    }    return 0;}C:#include #include int isPrime(int num) {    if (num <= 1) {        return 0;    }    for (int i = 2; i <= sqrt(num); i++) {        if (num % i == 0) {            return 0;        }    }    return 1;}int main() {    int num;    printf("Enter a number: ");    scanf("%d", &num);    if (isPrime(num)) {        printf("%d is a prime number.\n", num);    } else {        printf("%d is not a prime number.\n", num);    }    return 0;}Explanation:As the language was not mentioned in the question, I have written the code in different languages.PythonJavaC++CLogic of the code:Taking input from the userpassing the number through the function is_prime to check if the number is prime or notIf the number is less than or equal to 1, then return false, which means it is not a prime numberlooping from 2 to square root of n and we are checking if the each number is divisible with the number given.if the condition comes out to positive for atleast once, that means, it is divisible by some number i. Hence, return true which means it is a prime number. the loop ends if the no divisor is found, hence return false which means it is not a prime number.Sample test cases are verified and attached below for reference.
2 of 2
0
Explanation:Here is a simple Python program to check whether a number is prime or not:```def is_prime(n): if n <= 1: return False for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return Truenum = int(input("Enter a number: "))if is_prime(num): print(num, "is a prime number.")else: print(num, "is not a prime number.")```This program works as follows:1. The `is_prime` function checks whether a number `n` is prime or not.2. If `n` is less than or equal to 1, it returns `False`, because prime numbers are greater than 1.3. The function then checks divisibility from 2 to the square root of `n`. If `n` is divisible by any of these numbers, it returns `False`.4. If `n` is not divisible by any of these numbers, it returns `True`, indicating that `n` is a prime number.5. The program then prompts the user to enter a number and checks whether it is prime or not using the `is_prime` function.
🌐
w3resource
w3resource.com › c-programming-exercises › function › c-function-exercise-7.php
C Program: Check whether a number is a prime number or not - w3resource
October 24, 2025 - The function 'PrimeOrNot' takes a single argument 'n1' of type int. It checks whether the input number 'n1' is prime or not by iterating through all integers from 2 to n1/2. The function initializes a local integer variable i to 2 and enters ...
🌐
Talent Battle
talentbattle.in › important-coding-questions-for-placement › write-a-program-to-identify-if-the-number-is-prime-number-or-not
Program to identify if the number is Prime number or not | Talent Battle
April 26, 2023 - Home > Free Resources > Coding Questions 2023 > Program to identify if the number is Prime number or not ... Get a number as input from the user and check whether that number is prime or not.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › c program for prime numbers
C Program to Check Whether a Number Is Prime
April 21, 2026 - ... Initialize count = 0. Run an iterative loop from i = 2 to num - 1. For each i, check if num is divisible by i. If divisible, increment count. If num == 0 or num == 1, output "Not Prime".