C Program to Print A Pattern of Prime Numbers
Pattern to be printed looks like following, containing only prime numbers in regular sequence:
C program :
#include<stdio.h>
#include<conio.h>
int nextprime(int);
int isprime(int);
void main()
{
int i,p=1,n,j;
clrscr();
printf("Enter the no. of lines to be printed: ");
scanf("%d",&n);
if(n==0)
printf("\nINVALID INPUT");
printf("\n%4d\n",p);
if(n!=1)
{
for(i=2;i<=n;i++)
{
for(j=1;j<=i;j++)
{
p=nextprime(p);
printf("%4d",p);
}
printf("\n");
}
}
printf(" \n Visit www.EngineersBurger.com for more intresting stuff ! \n - Jay Akbari");
getch();
}
int nextprime(int s)
{
s=s+1;
while(!isprime(s) )
{
s=s+1;
}
return s;
}
int isprime(int s)
{
int i;
for(i=2;i<s;i++)
{
if(s%i==0)
return 0;
}
return 1;
}