Prime Number Program in C using for loop

In this post, we are going to make the Prime number program in c in the most straightforward way to understand.
In this post, we will make the Prime number program in c. It will help us to check whether the number is prime or not.

Prime number: A number not divisible by any other number except one is called a prime number.
E.g. 2,3,5,7,11 etc.

Table of Contents

Prime number program in C using for loop

In this program, we have used a function to check the Prime number in C. Return type of this function is bool type. 

It will return true if the number is prime and false if it is not.

In the "isPrime" function, we checked that this number is divisible by any number smaller than or equal to the square root of this number "n" using for loop.

The Time complexity of this program is O(sqrt(n)).[Because we are running a loop from 2 to the square root of n]

#include<stdio.h>
#include<stdbool.h>
bool isPrime(int n){
    if(n==1)
    return false;
    //for loop
    for(int i=2;i*i<=n;i++){
        if(n%i==0)
        return false;
    }
    return true;
}
int main() {
    int n;
    printf("\n Enter a number:");
    scanf("%d",&n);
    if(isPrime(n)==1)
        printf("\n %d is a Prime Number",n);
    else
        printf("\n %d is not a Prime Number",n);
    return 0;
}
Output:

Enter a number:573
573 is not a Prime Number

If you cannot run this program or get an error, let me know in the comment section.

If you need more optimized code for this topic, You can message us on our Instagram account. We will send you a meet link on Instagram and teach you every line of code for free of cost.

Video Solution to Check Prime number in C using for loop


Prime number program in c using while loop

In this program, we will check whether the number is prime or not using a while loop.
#include<stdio.h>
int main()
{
    int num,i=2;
    printf("\n Enter a number:");
    scanf("%d",&num);
    while(i<num)
    {
        if(num%i==0)
            break;
        i++;
    }
    if(i==num)
        printf("\n %d is a Prime Number",num);
    else
        printf("\n %d is not a Prime Number",num);
    return 0;
}

Approach to check number entered by the user is a prime number or not using the while loop:

  • We will declare two integer numbers one is num, and another is i.
  • We will initialize "i" with two, and the user enters the value of num.
  • In a while loop, we will perform num modulus "i" where i is less than num.  
  • If num%i comes to zero, it means that the number entered by the user is not a prime number.
  • If the value of num is equal to "i" then it is a prime number.

C program to check whether a number is prime or not using a do-while loop

#include<stdio.h>

int main()
{
    int num,i=2;
    printf("\n Enter a number:");
    scanf("%d",&num);
    do
    {
        if(num%i==0)
        {
            break;
        }
        i++;
    }while(i<num);
    if(i==num)
        printf("\n %d is a prime number",num);
    else
         printf("\n %d is not a prime number",num);

    return 0;
}

Approach to determine whether the number is prime or not using a do-while loop:

  • We declare an integer number and initialize its value from the user.
  • We are using a do-while loop to check whether the number is prime or not.
  • Use the if statement and modulus operator (it gives the remainder when we divide two numbers) to compare the remainder with zero; if the remainder comes to zero, the number is divisible by another number. Hence, it is not a prime number because it is divisible by another number. 
  • We print it is not the prime number.
  • If the value of i equals the number entered by the user, then we will print it as a prime number.

Write a program to check if a given number is prime or not using for loop.

#include<stdio.h>
#include<conio.h>
int main()
{
int num,i;
clrscr();
printf("\n Enter a number:");
scanf("%d",&num);
for(i=2;i<num;i++)
{
if(num%i==0)
{
printf("\n %d is not a prime number",num);
break;
}
}
if(i==num)
printf("\n %d is a prime number",num);
getch();
return 0;
}

Output:
Enter a number:11
11 is a prime number

Approach to determine whether the number is prime or not using for loop:

  • First, we declare an integer number.
  • Initialize its value from the user using scanf.
  • Now we are using for loop to divide that number from all those below that number and check the remainder.
  • Use the if statement to check the remainder. If the remainder comes to zero, the number is divisible by another number.
  • We have to print that the number entered by the user is not the prime number.
  • If the value of i equals the number entered by the user, then we will print it as a prime number.
About this post:
Siddharth Jha writes this post. It isn't easy, but I made this program the easiest way. So you can understand easily. You can also read an approach to make a Prime Number Program in C to check whether the number is prime or not, which is below all program output.

If you are having issues with these programs, please comment below.


Recommended post:
Odd and even numbers program in c
Simple calculator program in c using switch case
Matrix program in c using array

3 comments

  1. Thank you so much Siddharth. You explained me very clearly in every condition.
    1. Welcome bro

      Here is ur code

      #include
      #include
      int isPrime(int n){
      if(n==1)
      return 0;

      for(int i=2;i<=sqrt(n);i++){
      if(n%i==0)
      return 0;
      }
      return 1;
      }
      int main() {
      int n;
      scanf("%d",&n);
      if(isPrime(n)==1)
      printf("%d is a Prime Number",n);
      else
      printf("%d is not a Prime Number",n);
      return 0;
      }
  2. Hey!
    If you like this post please comment here.
Please do not enter any spam link in the comment box.