C Programs For Prime Number (Definition,Example,Program)

C Programs For Prime Number: In this article, we will know what is the prime number. and at the same time, we will know how to find the prime number in c as well as an example with explanation. We will perform this example in different ways.

 

C-Programs-For-Prime-Number

What is a prime number?


The prime number is the whole number. Which is greater than 1. Which is evenly dived from one or its own number. It is called a prime number.


Example: 2 is the prime number which is divided evenly from 1 or 2 itself.

Other example: 2,3,5,7,11, etc.

 

Prime Number Algorithm in c


  1. Start
  2. The user will get an integer variable X inter
  3. Will divide the variable with (X-1 to 2)
  4. If X is divisible with any value (X-1 to 2) then not prime
  5. else X is a prime number
  6. Stop


We will perform the program of a prime number in c in the following way.

  1. The prime number using Function
  2. Prime number program in c using for loop
  3.  Prime number program in c using while loop
  4. Print Prime Numbers from 1 to n

Prime Number Program  in C


The prime number using the function


#include <stdio.h>

 int prime_find (int);

main ()

 {

 int a, b;

  printf ("Enter Integer Number to Find Prime or Not. \ n");

 scanf ("% d", & a);

 b = prime_find (a);

  if (b == 1)

 printf ("% d is prime. \ n", a);

 else

 printf ("% d is not prime. \ n", a);

  return 0;

 }

  int prime_find (int x)

 {

 int y;

  for (y = 2; y <= x - 1; y ++)

 {

 if (x% y == 0)

 return 0;

 }

 return 1;

 }


Input

Enter Integer Number to Find Prime or Not.5

Output

5 is prime 


Prime number program in c using for loop


 #include <stdio.h>

 int main ()

 {

  int a, b, c = 0;

  printf ("Enter Integer Number to Find Prime or Not. \ n");

 scanf ("% d", & a);

  for (b = 2; b <= a / 2; ++ b)

 {

 // check for non prime number

 if (n% b == 0)

 {

 c = 1;

 break;

 }

 }

  if (c == 0)

 printf ("% d is a prime number.", a);

 else

 printf ("% d is not a prime number.", a);

  getch ();

 }

Input

Enter Integer Number to Find Prime or Not.5

Output

5 is a prime number


Prime number program in c using while loop


#include <stdio.h>

int main ()

{

 int n, i, count = 0;

 int a, b, c = 0;

 printf ("Enter Integer Number to Find Prime or Not. \ n");

 scanf ("% d", & a);

 b = 2;

 while (b <= a / 2)

 {

 // check for non prime number

 if (a% b == 0)

 {

 c = 1;

 break;

 }

 b ++;

 }

  if (c == 0)

 printf ("% d is a prime number.", a);

 else

 printf ("% d is not a prime number.", a);

  getch ();

 }

 

Input

Enter Integer Number to Find Prime or Not.5

Output

5 is a prime number


Print Prime Numbers from 1 to n


#include <stdio.h>

int main () {

 int a, b, count, n;

 printf ("Enter Rang For To Find Prime Number");

 scanf ("% d", & n);

 for (a = 1; a <= n; a ++) {

 count = 0;

 for (b = 2; b <= a / 2; b ++) {

 if (a% b == 0) {

 count ++;

 break;

 }

 }

  if (count == 0 && a! = 1)

 printf ("% d", a);

 }

  return 0;

}

Input

Enter Rang For To Find Prime Number 5

Output

2 3 5

Read Also: 

Patterns Programs in C 

Post a Comment

0 Comments