//10. C Program to generate n prime numbers and print them in pyramid pattern.
#include <stdio.h>
#include <conio.h>
void main()
{
int space, n, prime[100]; //Declare variables.
int count=0, i=2, j, k=2;
clrscr();
printf("Enter nth number:");
scanf("%d",&n);
while(i<=n+1)
{
for(j=2;j<=k-1;j++)
{
if(k%j==0) //Check whether k is divisible by any other number.
{
break;
}
}
if(j==k)
{
// printf("%d\n",k); //For debugging.
prime[count++]=k;
i++;
}
k++;
}
count=0;
space=n;
for (i=0;i<=n/2+1;i++)
{
for (k=0;k<space;k++)
{
printf(" "); //Provide 2 spaces
}
for (j=0;j<2*i-1;j++)
{
if(count<n)
printf("%d ",prime[count++]); //Provide 2 spaces after %d
}
space--;
printf("\n");
}
getch();
}
#include <stdio.h>
#include <conio.h>
void main()
{
int space, n, prime[100]; //Declare variables.
int count=0, i=2, j, k=2;
clrscr();
printf("Enter nth number:");
scanf("%d",&n);
while(i<=n+1)
{
for(j=2;j<=k-1;j++)
{
if(k%j==0) //Check whether k is divisible by any other number.
{
break;
}
}
if(j==k)
{
// printf("%d\n",k); //For debugging.
prime[count++]=k;
i++;
}
k++;
}
count=0;
space=n;
for (i=0;i<=n/2+1;i++)
{
for (k=0;k<space;k++)
{
printf(" "); //Provide 2 spaces
}
for (j=0;j<2*i-1;j++)
{
if(count<n)
printf("%d ",prime[count++]); //Provide 2 spaces after %d
}
space--;
printf("\n");
}
getch();
}
No comments :
Post a Comment