C program to find prime number

devquora
devquora

Posted On: Dec 23, 2020

C program to find prime number

 

Overview:

In this article, we will introduce you with a Prime number. Then I will Define factors of a number. Moreover, I will show a visual representation of prime numbers. Furthermore, I will demonstrate to check the prime number with some examples. Then, I will show you the logic to check if the given number is prime or not. After all, I will show a C Program to find prime numbers using 3 way i.e. (1) C program to find whether a given number is prime number or not according to the user's choice with output. (2) C program to find whether the entered number is prime number or not by using a function with output. (3) C program to find prime numbers in the range with output. After that, I will explain the internal execution of the program.

Table of contents:

  1. What is a Prime Number?
  2. What do you mean by Factor of a number?
  3. Visual representation of Prime Numbers
  4. Is 0 and 1 a prime number or not?
  5. Demonstration to check prime number with some examples
  6. Main Logic to check if the given number is prime or not
  7. C program to find given number is prime number or not according to the user's choice with output
  8. C program to find that the entered number is prime number or not by using function with output
  9. C program to find prime numbers upto limit with output
  10. Conclusion

What is a Prime Number?

A Prime Number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. First few prime numbers are : 2, 3, 5, 7, 11, 13, 17, 19, 23, etc.

In other words, Prime numbers are the positive integers having only two factors, 1 and the integer itself. For example, factors of 8 are 1,2,4 and 8. But factors of 11 are only 1 and 11. Hence, 11 is a prime number but 8 is not the prime number.  1 is never a prime number.

What do you mean by Factor of a number?

Factors are made up of divisor and dividend. In other words you can say that the factors are the numbers from whom a number is divisible. For instance, Factors of 23 ia 1 and 23 but 1, 2, 3, 4, 24 are the factors of the 24.

Visual representation of Prime Numbers

Demo of Prime

Is 0 and 1 a prime number or not?

No, Neither 0 nor 1 is a prime number.

Demonstration to check prime number with some examples

Demo Of Prime

Main Logic to check if the given number is prime or not

  • Step 1: Take a number and store it into an integer variable with int datatype.
  • Step 2: Divide the variable by from 2 to itself.
  • Step 3: If the variable is divisible by 2 or rather than 1 and itself. Then, It is not prime.
  • Step 4: Else it is prime.

C program to find given number is prime number or not according to the user's choice with output

#include <stdio.h>
#include <conio.h>
int main()
{
      int num, count, temp = 0;
      char ch;
	  while(1){
      printf("\nPlease! Enter a Number:\t");
      scanf("%d", &num);
      if(num==1)
      {
      	printf("\n%d is Not a Prime Number\n", num);
	  }
	  else
	  {
      for(count = 2; count <= num/2; count++)
      {
            if(num%count == 0)
            {
                  temp = 1;
                  break;
            }
      }
      if(temp == 0)
      {
            printf("\n%d is a Prime Number\n", num);
      }
      else
      {
            printf("\n%d is Not a Prime Number\n", num);
      }
      
    /*printf("Do you wish to continue the process? (y/n)\n");
    scanf("%c", &ch);*/
 }
    
    }
      return 0;
}

The output of the Program:

Demo of PrimeChoice

C program to find that the entered number is prime number or not by using function with output

#include <stdio.h>
#include <conio.h>

int checkPrime(int);
int main()
{
   int n,i, result;
   while(1) {
   printf("Enter an integer to check whether it is prime or not.\n");
   scanf("%d",&n);
  
   result = checkPrime(n);
 
   if ( result == 1 )
      printf("%d is not prime.\n", n);
   else
      printf("%d is prime.\n", n);} 
   return 0;
} 
  int checkPrime(int number) {
   int i,count = 0;
   for(i=2; i<=number/2; i++)
   {
       if(number%i == 0)
       {
         count=1;
         break;
       }
   }
   if(number == 1) count = 1;
   return count; }
 

The output of the Program:

Demo of Prime using function

C program to find prime numbers upto limit with output

#include <stdio.h>
#include <math.h>
void prime(int limit, int arr[])
{
     int m, n;
     for(m = 0; m < limit; m++){
	 arr[m] = 1;  }
     arr[0] = 0, arr[1] = 0; 
     for(m = 2; m < sqrt(limit); m++)
	{ 
         for (n = m*m; n < limit; n = n + m) 
		{
             arr[n] = 0; 
		} 
	}
}
int main()
{
     int count, limit;
	 printf("\nEnter The Limit:\t");
	 scanf("%d", &limit);
     int arr[limit];
     prime(limit, arr);
	 printf("\n");
     for (count = 0; count < limit; count++)
	{
		if (arr[count] == 1) 
		{
			printf("%d\t", count);
		}	
	}
	printf("\n");
	return 0;
}

The output of the Program:

Demo of Prime up to given limit

Conclusion:

This Conclusion shows the Explanation of the Program with output.

The internal working of this program depends upon the previous Demonstration to check prime numbers with some examples.

This Program stores an integers into the variable with int datatype. After it is used for the loop it checks all divisors between 2 and number itself. Then, Compare it is divisible or not by any number rather than 1 and itself. At last If user input 1 then it shows the 1 is prime number. Next, If user inter 23 then it display 23 is prime number and Display to enter another number.

In the Program of prime upto limit, It wants to enter the limit if users enter 11 then After the successful compilation of the program, it displays all prime numbers up to the limit 11. It starts from 2 and ends on 11 because we know that the first prime number starts from 2.

    Related Questions

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C program to add two numbers

    This Program stores two integers into the int datatype by using to variables. After that store the sum of these two variables into another variable and display it on the screen.After the successfu..