Check value

Example program to check whether a Number is Prime or Not in C

Example  program for check whether a Number is Prime or Not in C

In this article, we will discuss the concept of the Example  program to check whether a Number is Prime or Not in C programming language

In this post, we are going to learn how to check whether a number is prime or not from the given number using for, while and do-while loop in C language

 

Prime number

The number that can be divided by 1 and itself. it is called prime number

for Example 2,3,5,7,11,13…

Check prime or not

To understand this example programs, you should have previous knowledge of following C topics

for loop in C language

while loop in C language

do-while loop in C language

If statements in C  language

 

Check the prime number using for loop

Program 1

This program allows the user to enter a positive number and then it will check the given number is a prime number or not using for loop in C language

 

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

int main()
{
    int num,i,count=0;

    printf("Enter the positive integer\n");
    scanf("%d",&num);
    for(i=2; i<=num/2; i++){

    //condition for non-prime
    if(num%i==0)
    {
        count=1;
        break;
    }
}

if(num==1){
    printf("you entered %d\n",num);
    printf("%d is neither a prime nor a composite number ",num);
}
else{
        if(count==0){
            printf("you entered %d\n\n",num);
            printf("%d is a prime number ",num);
        }
        else{
             printf("you entered %d\n",num);
             printf("%d is not a prime number ",num);
        }
}
getch();
    return 0;
}

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

Case 1

Enter the positive integer
1
you entered 1
1 is neither a prime nor a composite number


Case 2

Enter the positive integer
38
you entered 38
38 is not a prime number


Case 3

Enter the positive integer
29
you entered 29
29 is a prime number

 

Program 2

Check the prime number using while loop

This program allows the user to enter a positive number and then it will check the given number is a prime number or not using while loop in C language

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

int main()
{
    int num,i,count=0;

    printf("Enter the positive integer\n");
    scanf("%d",&num);

    i=2;
    while(i<=num/2){

    //condition for non-prime
    if(num%i==0)
    {
        count=1;
        break;
    }
    i++;
}

if(num==1){
        printf("you entered %d\n",num);
    printf("%d is neither a prime nor a composite number ",num);
}
else{
        if(count==0){
            printf("you entered %d\n\n",num);
            printf("%d is a prime number ",num);
        }
        else{
             printf("you entered %d\n",num);
             printf("%d is not a prime number ",num);
        }
}
getch();
    return 0;
}

 

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

Case 1

Enter the positive integer 1
you entered 1
1 is neither a prime nor a composite number

 

Case 2

Enter the positive integer
20
you entered 20
20 is not a prime number

 

Case 3

Enter the positive integer
13
you entered 13
13 is a prime number

 

 

Check the prime number using Do-while loop

Program 3

This program allows the user to enter a positive number and then it will check the given number is a prime number or not using the do-while loop in C language

 

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

int main()
{
    int num,i,count=0;

    printf("Enter the positive integer\n");
    scanf("%d",&num);

    i=2;
    do{

    //condition for non-prime
    if(num%i==0)
    {
        count=1;
        break;
    }
    i++;
}while(i<=num/2);

if(num==1){
        printf("you entered %d\n",num);
    printf("%d is neither a prime nor a composite number ",num);
}
else{
        if(count==0){
            printf("you entered %d\n\n",num);
            printf("%d is a prime number ",num);
        }
        else{
             printf("you entered %d\n",num);
             printf("%d is not a prime number ",num);
        }
}
getch();
    return 0;
}

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

Case 1

Enter the positive integer 1
you entered 1
1 is neither a prime nor a composite number

 

Case 2

Enter the positive integer
12
you entered 12
12 is not a prime number

 

Case 3

Enter the positive integer
7
you entered 7
7 is a prime number

 

Suggested for you

The operator in C language

Data type in C language

Variable in C language

input-output function in C language

 

Similar post

Program to check whether a Number is Prime or Not in C++

Program to check whether a Number is Prime or Not in C

Program to check whether a Number is Prime or Not in Java

Program to check whether a Number is Prime or Not in Python

 

 

 

 

Example for Python program to check Prime number
Example program to check whether a Number is Prime or Not in Java
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago