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.
Logic :
Prime Numbers : Numbers that are divisible by 1 and number itself is called Prime number.
- This program is nothing but the pyramid of the prime numbers .
- To Evaluate Number is Prime or not Consecutively divide that number from2 to n/2
- 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.
- 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:
Post a Comment