Pages

Generating Prime Number in C

This is an example of generating prime number. It takes input form user and generate prime number upto given number. The main logic behind finding the prime number is to count the no integer that divide the number and if the count is 2 then we print number as prime number else we leave it.

prime_number.c

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

void main(){
int num, i, j, c = 0;
printf("Enter number to generate Prime number : ");
scanf("%d",&num);

for(i = 1; i <= num; i++)
{
     for(j = 0; j <= i; j++)
     {
           if( i % j == 0)
               c++;
      }


     if(c == 2)
           printf("%d \t", i); //print prime number
     c = 0;
}

getch();
}

@msucil

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment