Friday, November 21, 2014

C print prime number



C Program to Print Number Pyramid Pattern

 #include
#include
 int main()
{
   int i, num, j, xpos = 30, ypos = 10;
   clrscr();
    printf("enter n (between 2 & 9) : ");
   scanf("%d", &num);
    for (i = 1; i <= num; i++)
   {
      gotoxy(xpos, ypos);
      for (j = 1; j <= i; j++)
         printf("%d ", i);
      xpos = xpos - 1;
      ypos++;
   }
    return (0);
}

Output

Enter n (between 2 & 9) : 9
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9

C Program to Print Even Number Pyramid


#include
#include
 int main()
 {
   int i, j, num = 2;

   for (i = 0; i < 4; i++)
   {
      num = 2;
      for (j = 0; j <= i; j++)
      {
         printf("%d\t", num);
         num = num + 2;
      }
      printf("\n");
   }
    return (0);
}

C Program to Print prime number Pyramid


#include
#include 
int prime(int num);
int main()
{
   int i, j;
   int num = 2;

   for (i = 0; i < 5; i++)
 {
      printf("\n");
      for (j = 0; j <= i; j++)
    {
         while (!prime(num))
        {
            num++;
         }
         printf("%d\t", num++);
      }
   }
   return (0);
}
 int prime(int num)
 {
   int i, flag;
   for (i = 2; i < num; i++)
 {
      if (num % i != 0)
         flag = 1;
      else
 {
         flag = 0;
         break;
      }
   }

   if (flag == 1 || num == 2)
      return (1);
   else
      return (0);
}

Output

2
3       5
7       11      13
17      19      23      29
31      37      41      43      47

No comments:

Post a Comment