Prime Number Program in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Prime number program in C | Number having only two factors ( 1 and itself ) called Prime number. Example:- 5, 7, 11, 13…

The problem to write C program to find the prime number is based on to find factors of a number.

Note:- We don’t have negative prime numbers. A positive integer is prime if it has only two divisors among the positive integers. Zero is neither positive nor negative, hence it is not a prime number. 1 is not considered prime numbers. 2 is the only even prime number.

All negative numbers:- not prime number
0:- not a prime number
1:- not a prime number

We know that prime number having only two factors, so if the number is divisible by any number between 2 to half of the number then the number is not a prime number.

A number will never divisible by the greater than half of the number. For example, half of the number 10 is 5 so, the 10 is never divisible by greater than 5 (except itself). Hence we should check only for the numbers 2 to half of the number.

It will reduce the number of iteration of the loops, and the performance of the program will increase.

#include<stdio.h>
int main()
{
  int number,count=0 ;

  printf("Enter number: ");
  scanf("%d",&number);

  for(int i=2; i<=number/2; i++)
  {
    if(number%i==0)
    {
      count=1;
      break;
    }
  }

  if(count == 1 || number == 1)
      printf("%d is not a prime number\n",number);
  else
      printf("%d is a prime number\n",number);

  return 0;
}

Output:-

Enter number: 1
1 is not a prime number

Enter number: 2
2 is a prime number

Enter number: 4
4 is not a prime number

In the above prime number program in C, we had taken a temporary variable count and initialize it with 0. Now initialize iteration variable i with 2 and iterate until number/2 using update expression i++. If the number is divisible at the time of iteration then the value of the variable i updated with 1 and due to control statement break, the control of the program came out of the loop.

Now, if the count or number is 1 then the number is not a prime number else number is a prime number.

Prime number program in C using while loop

While loop in C is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

 #include<stdio.h>
 int main()
 {
   int number, count=0, i=2;

   printf("Enter a positive interger: ");
   scanf("%d", &number);

   if(number == 1) count = 1;
   else
   {
     while(i <= number/2)
     {
       if(number%i == 0)
       {
         count=1;
         break;
       }
       i++;
     }
   }

   count == 1 ? printf("Not Prime") : printf("Prime");

   return 0;
 }

In the above prime number program in C, we check the number is 1 or not. If the number is 1 the update the count variable with 1 else iterate the while loop.

At the time of iteration if the number is divisible by the variable i then the value of the count variable will updated and due to the control statement break the control of the program came out of the loop.

Now, using a ternary operator check the value of the count variable. If the value of the count variable is 1 then it is not a prime number else it is a prime number.

Find Prime Number in a Given Range

The below prime number program in C displays all prime numbers in a given range.

#include<stdio.h>
int main()
{
  int m, n, i, j, count;

  // The value of m and n should be +ve and m<n
  printf("Enter m and n Values(m<n): ");
  scanf("%d %d",&m,&n);

  // 1 is not a prime number, so check from 2
  if(m == 1) m++;

  printf("Prime numbers from %d to %d are: \n", m, n);

  for(i=m;i<=n;i++)
  {
      count=0;
      for(j=2;j<=i/2;j++)
      {
     if(i%j==0)
     {
       count=1;
       break;
     }
      }

      if(count==0)
     printf("%d\t", i);
  }

  return 0;
}

Output:-

Enter m and n Values(m<n): 1 50
Prime numbers from 2 to 50 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Enter m and n Values(m<n): 50 100
Prime numbers from 50 to 100 are:
53 59 61 67 71 73 79 83 89 97

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Comment

Your email address will not be published. Required fields are marked *