| | |
Print Prime Numbers(using for loop)
![]() |
•
•
Join Date: Apr 2006
Posts: 5
Reputation:
Solved Threads: 0
It is my while loop program:
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> void main() { int count,i=1; int a; clrscr(); while(i<=500) { count=0; a=1; while(a<=i) { if(i%a==0) count++; a++; } if(count==2) printf("%d\t",i); i++; } getch(); }
Last edited by Dave Sinkula; Apr 23rd, 2006 at 5:35 pm.
Here you go. Added for loop in place of while, and commented out the i++ at end of while loop.
Also added int variable col, to print a newline every ten numbers printed to standard out.
Also added int variable col, to print a newline every ten numbers printed to standard out.
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> int main(int argc, char *argv[]) { int count,i=1; int a; int col=0; //while(i<=500) for(i=1; i<501; i++) { count=0; a=1; while(a<=i) { if(i%a==0) count++; a++; } if(count==2){ printf("%d\t",i); col++; if(col%10==0) printf("\n"); } //i++; } system("PAUSE"); return 0; }
> I make this program but with while loop then i was told to make it using for loop, which i tried but failed.
So turn
To
So turn
C Syntax (Toggle Plain Text)
i = 0; while ( i < 500 ) { // do stuff i++; }
C Syntax (Toggle Plain Text)
for ( i = 0 ; i < 500 ; i++ ) { // do stuff }
DOH! Salem
Johns' while loop was: less than OR EQUAL to 500.
So change to for i < 501 NOT 500.
This is Johns' future your playing with, pay attention.
Johns' while loop was: less than OR EQUAL to 500.
So change to for i < 501 NOT 500.
This is Johns' future your playing with, pay attention.
•
•
Join Date: Apr 2006
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by john_hasan
Friends i have little bit problem in making a c language program.
I have to make a program which prints prime numbers from 1 to 500.
I make this program but with while loop then i was told to make it using for loop, which i tried but failed.
So kindly help me. Thanks
from,
john(pakistan)
C Syntax (Toggle Plain Text)
int flag=1; for(int i=2;i<500;i++) //since 1 and 500 are not prime { flag=1; for(int j=2;j<i/2;j++) //since a no. cannot be divisible by a { //no. greater than its half. if(i%j==0) //if i is divisible by any no. between 2 & i/2 { flag=0; break; } } if(flag==1) printf("\n",i); }
Last edited by Dave Sinkula; Apr 25th, 2006 at 10:50 am.
Ok here's my best shot. The key rule for a prime number is:
It has precisely two positive integer factors.
So the neatest solution I can think of counts those and quits the for loop as soon as it's found more than 2 or reached half way to n.
Can anyone better it?
It has precisely two positive integer factors.
So the neatest solution I can think of counts those and quits the for loop as soon as it's found more than 2 or reached half way to n.
Can anyone better it?
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; static const int MAX = 500; static const int MIN = 0; static const int MAXCOL = 10; bool isPrime(int r); int main(int argc, char *argv[]) { int col = 0; int n; for(n=MIN; n<=MAX; n++) if(isPrime(n)){ cout << n << '\t'; col++; if(col == MAXCOL){ cout << '\n'; col = 0; } } system("PAUSE"); return 0; } bool isPrime(int n) { if((n == 0)||(n == 1)) return false; int factors = 2; for(int i=2; i<=((int)n/2); i++){ if(n%i == 0){ factors++; break; } } if(factors == 2) return true; return false; }
![]() |
Similar Threads
- Help with prime numbers (C)
- C++ Finding Prime Numbers (C++)
- (Noob) need some help w/ Prime Numbers (C++)
- prime numbers (C++)
Other Threads in the C Forum
- Previous Thread: game 2d array search help
- Next Thread: Had a homework problem in c programming and need help!!
| Thread Tools | Search this Thread |
adobe ansi api array asterisks binarysearch calculate centimeter char character convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax directory feet fflush file floatingpointvalidation fork forloop frequency givemetehcodez global grade graphics gtkgcurlcompiling hacking highest homework i/o inches infiniteloop interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. match meter microsoft mysql number odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv recvblocked repetition research scanf scheduling segmentationfault send sequential single socket socketprograming socketprogramming stack standard string suggestions systemcall threads turboc unix urboc user variable voidmain() wab win32api windows.h






