Open In App

C Program To Check Prime Number By Creating a Function

Last Updated : 24 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Prime numbers have only 2 factors, 1 and themselves. For example, 2,3, 5, 7, 11,… are the first 5 prime numbers. Here we will build a C program to check prime numbers by creating a function.

Approaches to Check Prime number

using 3 different approaches:

  1. Using for loop for prime number.
  2. Using for loop for not prime number.
  3. Using while loop for prime number.

Example:

Input: n = 7

Output: 7 is a prime number

1. Using For Loop

Below is the C program to check prime numbers using for loop:

C




// C program to demonstrate whether
// a number is prime or not using 
// for loop
#include <stdio.h>
  
// Defining the function
int primenumber(int number)
{
    int i;
    
    // Condition for checking the
    // given number is prime or
    // not
    for (i = 2; i <= number / 2; i++) 
    {
        if (number % i != 0)
            continue;
        else
            return 1;
    }
    return 0;
}
  
// Driver code
int main()
{
    int num = 7, res = 0;
    
    // Calling the function
    res = primenumber(num);
    if (res == 0)
        printf("%d is a prime number", num);
    else
        printf("%d is not a prime number", num);
}


Output

7 is a prime number

2. Using For Loop for Not Prime Number

Below is the C program to check whether a number is prime or not using for loop:

C




// C program to demonstrate whether
// a number is prime or not using 
// for loop
#include <stdio.h>
  
// Defining the function
int primenumber(int number)
{
    int i;
    
    // Condition for checking 
    // the given number is prime
    // number or not
    for (i = 2; i <= number - 1; i++) 
    {
        if (number % i == 0)
            return 0;
    }
    return 1;
}
  
// Driver code
int main()
{
    int num = 4, res;
    
    // Calling the function
    res = primenumber(num);
  
    if (res == 1)
        printf("%d is a prime number", num);
    else
        printf("%d is not a prime number", num);
  
    return 0;
}


Output

4 is not a prime number

3. Using While Loop

Below is the C program to check whether a number is prime or not using a while loop:

C




// C program to demonstrate whether
// a number is prime or not using
// while loop
#include <stdio.h>
  
// Defining the function
int primenumber(int number)
{
  
    // Condition for checking the 
    // given number is prime or
    // not
    int i = 2;
    while (i <= number / 2) 
    {
        if (number % i == 0)
            return 0;
        else
            i++;
    }
    return 1;
}
  
// Driver code
int main()
{
    int num = 7, prime;
    
    // Calling the function
    prime = primenumber(num);
    
  // Printing the result
    if (prime == 1)
        printf("%d is a prime number", num);
    else
        printf("%d is not a prime number", num);
    return 0;
}


Output

7 is a prime number


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads