- Back to Home »
- C , c++ , code , coding , generate , number , prime , programming , project c++(basic) »
- Coing to Generate prime Number in c

Posted by : Waqas Javed
Monday, 22 December 2014

#include<stdio.h>;

#include<conio.h>;

//code by waqas javed //main

void main() {

//initializing vars

int i = 1; //prime number founded

int j = 2; //chk start point

int l = 1; //var factor chk var

int m = 0; //number of factor

int number = 0;

//user tell how many prime number to generate

printf("Enter how many prime number you want to generate: ");

scanf("%d", &number);

//printing first 20 prime no.

while (i <= number){

//chk for factors of number

while (j > l){

//if number is factor

if (j%l == 0){

m++;

l++;

}

//if not

else {

l++;

}

}

//chk and print if prime no.

if (j == l) {

//if prime

if (m == 1)

printf("%d.\t%d \n", i++, j);

j++;

l = 1;

m = 0;

}

//if not reinitializing value to chk nxt var

else {

j++;

l = 1;

m = 0;

}

}

_getch();

}

This code work as it starts from 2 and check whether it is a even number or not.. Its checking is designed like the program checks all the number below that number to be checked that whether they are its factor are not... Then the program check how many factor the number has if they are 2 then it take it as prime number and print it..

For example: Program is checking 9 is a prime no or not. it will check all number below it that is 1,2,3,4,5,6,7,8 now the we have 1 and 3 as its factor and 9 itself also so total factor exceeded 2 and the number is not prime...... similarly it will keep checking all number in increasing order starting from 1 until the total prime number too be founded are found.

#include<conio.h>;

//code by waqas javed //main

void main() {

//initializing vars

int i = 1; //prime number founded

int j = 2; //chk start point

int l = 1; //var factor chk var

int m = 0; //number of factor

int number = 0;

//user tell how many prime number to generate

printf("Enter how many prime number you want to generate: ");

scanf("%d", &number);

//printing first 20 prime no.

while (i <= number){

//chk for factors of number

while (j > l){

//if number is factor

if (j%l == 0){

m++;

l++;

}

//if not

else {

l++;

}

}

//chk and print if prime no.

if (j == l) {

//if prime

if (m == 1)

printf("%d.\t%d \n", i++, j);

j++;

l = 1;

m = 0;

}

//if not reinitializing value to chk nxt var

else {

j++;

l = 1;

m = 0;

}

}

_getch();

}

This code work as it starts from 2 and check whether it is a even number or not.. Its checking is designed like the program checks all the number below that number to be checked that whether they are its factor are not... Then the program check how many factor the number has if they are 2 then it take it as prime number and print it..

For example: Program is checking 9 is a prime no or not. it will check all number below it that is 1,2,3,4,5,6,7,8 now the we have 1 and 3 as its factor and 9 itself also so total factor exceeded 2 and the number is not prime...... similarly it will keep checking all number in increasing order starting from 1 until the total prime number too be founded are found.