Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
C++ code to check whether the number is prime or not

C code to check whether the number is prime or not

Posted on October 15, 2020October 15, 2020

Table of Contents

  • C code to check whether the number is prime or not
    • Code to check whether the number is prime or not using for loop
    • Code to check whether the number is prime or not using while loop
    • Code to check whether the number is prime or not using do-while loop
    • Code to check whether the number is prime or not using function
    • C code to check whether the number is prime or not -using recursion

C code to check whether the number is prime or not

In this tutorial, we will discuss the C code to check whether the number is prime or not

In this post, we are going to learn how to check whether the given number is prime or not  using 5 ways in C language

This is done using for loop, while loop, do-while loop function and recursion

 

A prime number is a number which is greater than(positive) one and divisible by only two numbers: 1 and it self. when any number is divisible by any other number it is not a prime number.

So, prime numbers can,t be divided by other numbers than itself or 1 Eg 2,3,5,7,11,13,17 ………..

Code to check whether the number is prime or not using for loop

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i,count=0;//variable declaration
    printf("Enter a positive integer\n");
    scanf("%d",&num);//Takes input from the user

    for(i=1; i<=num; ++i){
        if(num%i==0){
            count++;

        }
    }

    if(num==1){
        printf("1 is neither prime or composite");
    }
    else{
         if(count==2){
        printf("%d is a prime number",num);
    }
    else{
        printf("%d is not a prime number",num);
    }
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a positive integer
17
17 is a prime number

Case 2

Enter a positive integer
24
24 is not a prime number

Case 3

Enter a positive integer
1
1 is neither prime or composite

 

In this program,

  • integer variable num,i  are declared.
  • integer variable count=0  is declared and initialized
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using scanf() function and store to the variable num
  • Create a for loop of i from 1 to n and increase the value of i after every iteration by 1
  • Calculate mod of every value of i and check whether the result is equal to zero or not
  • when the if statement returns true then the count value is increased by 1 until the test condition becomes false
  • The given numbers are tested whether it is prime or not using the count value
  • if the value of the count is equal to 0 then the number is prime else  the number is composite
  • Then,the program is displayed the output using printf() function.

 

Code to check whether the number is prime or not using while loop

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i,count=0;
    printf("Enter a positive integer\n");
    scanf("%d",&num);
i=1;
    while(i<=num){
        if(num%i==0){
            count++;

        }
         ++i;
    }

    if(num==1){
        printf("1 is neither prime or composite");
    }
    else{
         if(count==2){
        printf("%d is a prime number",num);
    }
    else{
        printf("%d is not a prime number",num);
    }
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a positive integer
47
47 is a prime number

Case 2

Enter a positive integer
39
39 is not a prime number

Case 3

Enter a positive integer
1
1 is neither the a prime or composite

 

In this program,

  • integer variable num,i  are declared.
  • integer variable count=0  is declared and initialized
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using scanf() function and store to the variable num
  • Create a while loop of i from 1 to n and increase the value of i after every iteration by 1
  • Calculate mod of every value of i and check whether the result is equal to zero or not
  • when the if statement returns true then the count value is increased by 1 until the test condition becomes false
  • The given numbers are tested whether it is prime or not using the count value
  • if the value of the count is equal to 0 then the number is prime else  the number is composite
  • Then,the program is displayed the output using printf() function.

Code to check whether the number is prime or not using do-while loop

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i,count=0;
    printf("Enter a positive integer\n");
    scanf("%d",&num);
i=1;
   do{
        if(num%i==0){
            count++;

        }
         ++i;
    } while(i<=num);

    if(num==1){
        printf("1 is neither prime or composite");
    }
    else{
         if(count==2){
        printf("%d is a prime number",num);
    }
    else{
        printf("%d is not a prime number",num);
    }
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a positive integer
83
83 is a prime number

Case 2

Enter a positive integer
91
91 is not a prime number

Case 3

Enter a positive integer
1
1 is neither the a prime or composite


In this program,

  • integer variable num,i  are declared.
  • integer variable count=0  is declared and initialized
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using scanf() function and store to the variable num
  • Create a do-while loop of i from 1 to n and increase the value of i after every iteration by 1
  • Calculate mod of every value of i and check whether the result is equal to zero or not
  • when the if statement returns true then the count value is increased by 1 until the test condition becomes false
  • The given numbers are tested whether it is prime or not using the count value
  • if the value of the count is equal to 0 then the number is prime else  the number is composite
  • Then,the program is displayed the output using printf() function.

 

Code to check whether the number is prime or not using function

Program 4

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,result=0;
    printf("Enter a number\n");
    scanf("%d",&num);
    result=primeNum(num);
    if(result==0)
         printf("%d is a prime number",num);
    else
        printf("%d is not a prime number",num);
               getch();
    return 0;
}
    int primeNum(int n)
    {
        int i;
        for(i=2; i<=n/2; i++)
        {
            if(n%2!=0)
                continue;
            else
                return 1;
        }
        return 0;
    }

When the above code is executed, it produces the following result

Case 1

Enter a positive integer
29
29 is a prime number

Case 2

Enter a positive integer
50
50 is not a prime number

 

C code to check whether the number is prime or not -using recursion

Program 5

#include <stdio.h>
#include <stdlib.h>
int isPrime(int,int);//function prototype
int main()
{
    int num,result;
    printf("Enter a positive number\n");
    scanf("%d",&num);
    result=isPrime(num,num/2);
    if(result==1)
         printf("%d is a prime number",num);
    else
        printf("%d is a composite number",num);
               getch();
    return 0;
}
    int isPrime(int n,int i)
    {
            if(i==1)
                return 1;
            else{
                    if(n%i==0)
                return 0;
            else
                isPrime(n,i-1);

            }
    }

When the above code is executed, it produces the following result

Case 1

Enter a positive integer
19
19 is a prime number

Case 2

Enter a positive integer
24
24 is a composite number

 

Suggested post

for loop in C language

while loop in C language

do-while loop in C language

if statements in C language

function in C language

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes