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();
}
#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();
}
No comments:
Post a Comment