About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Tuesday, September 3, 2013

C Program to Print prime number Pyramid

Print prime number Pyramid in C Pyramid Program Home
Logic :
Prime Numbers : Numbers that are divisible by 1 and number itself is called Prime number.
  1. This program is nothing  but the pyramid of the prime numbers .
  2. To Evaluate Number is Prime or not Consecutively divide that number from2 to n/2
  3. Example : to find whether 17 is prime or not divide that number from 2 to (17/2 = 8) if none of the remainder is zero then the number is surely prime else number is non-prime.
  4. Remaining Logic is same as Any other Nested Pyramid


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

int prime(int num);

void main()
{
int i,j;
int num=2;
clrscr();

  for(i=0;i<5;i++)
  {
    printf("n");
       for(j=0;j<=i;j++)
       {
          while(!prime(num))
               num++;
       printf("%d\t",num++);
       }
  }
getch();
}
//--------------------------------------------
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: