the following is a program to print prime numbers after taking user input of the number of prime numbers the user wants to print.. please correct the mistakes in this program.. also tell me if there are any mistakes in the programing style or any area where i need to work more.. Code: #include<stdio.h> #include<conio.h> int main() { int num; printf("Enter number.."); scanf("%d", &num); if(num==1) printf("\n1"); else if(num==2) { printf("\n1\n2"); } else { printf("\n1\n2\n"); int divisor,i,rem; for(i=3;i<num;i++) { divisor=2; while(rem!=0 && divisor<i) { { rem=i%divisor; divisor++; } if(rem!=0) { printf("%d\n", i); } else continue; } } } getch(); return 0; }
Code: void main() { int reqprimes, primesfound, isprime ; long testno, testfac; printf("no of primes to check : "); scanf("%d", &reqprimes); for( primesfound = 0, testno = 1; primesfound <= reqprimes; testno++ ) { isprime = 1; for( testfac = 2; testfac * testfac <= testno && isprime; testfac++ ) { if( testno % testfac == 0 ) isprime = 0; } if( isprime ) { printf("%ld is prime\n", testno); primesfound++; } } }
yar boss it is very complicated programme... i have corrected, but i am giving any number it is display only 1,2,3... it is simple prime no.. program. Code: #include<stdio.h> #include<conio.h> void main(void) { clrscr(); int i, num=1; while(num<=1000) { i=2; while(i<=num) { if(num%i==0) break; i++; } if(i==num) printf("%3d, ", num); num++; } getch(); }
eriyer has the perfect solution for it with his code fully optimized in order to have minimum iteration to find the list of primes.