Print Prime Numbers(using for loop)

Reply

Join Date: Apr 2006
Posts: 5
Reputation: john_hasan is an unknown quantity at this point 
Solved Threads: 0
john_hasan john_hasan is offline Offline
Newbie Poster

Print Prime Numbers(using for loop)

 
0
  #1
Apr 22nd, 2006
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)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: Print Prime Numbers(using for loop)

 
0
  #2
Apr 22nd, 2006
No problem, just show us what you manage to do so far...
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5
Reputation: john_hasan is an unknown quantity at this point 
Solved Threads: 0
john_hasan john_hasan is offline Offline
Newbie Poster

Re: Print Prime Numbers(using for loop)

 
-1
  #3
Apr 22nd, 2006
It is my while loop program:
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int count,i=1;
  6. int a;
  7. clrscr();
  8. while(i<=500)
  9. {
  10. count=0;
  11. a=1;
  12. while(a<=i)
  13. {
  14. if(i%a==0)
  15. count++;
  16. a++;
  17. }
  18. if(count==2)
  19. printf("%d\t",i);
  20. i++;
  21. }
  22. getch();
  23. }
Last edited by Dave Sinkula; Apr 23rd, 2006 at 5:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Print Prime Numbers(using for loop)

 
0
  #4
Apr 22nd, 2006
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.

  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. int count,i=1;
  7. int a;
  8. int col=0;
  9.  
  10. //while(i<=500)
  11. for(i=1; i<501; i++)
  12. {
  13. count=0;
  14. a=1;
  15.  
  16. while(a<=i)
  17. {
  18. if(i%a==0)
  19. count++;
  20. a++;
  21. }
  22.  
  23. if(count==2){
  24. printf("%d\t",i);
  25. col++;
  26. if(col%10==0)
  27. printf("\n");
  28. }
  29. //i++;
  30. }
  31.  
  32. system("PAUSE");
  33. return 0;
  34. }
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Print Prime Numbers(using for loop)

 
0
  #5
Apr 22nd, 2006
> 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
  1. i = 0;
  2. while ( i < 500 ) {
  3. // do stuff
  4. i++;
  5. }
To
  1. for ( i = 0 ; i < 500 ; i++ ) {
  2. // do stuff
  3. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Print Prime Numbers(using for loop)

 
0
  #6
Apr 22nd, 2006
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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Print Prime Numbers(using for loop)

 
0
  #7
Apr 22nd, 2006
I didn't pay any attention to his program, it wasn't formatted with code tags.

All I did was illustrate the relationship between for loops and while loops, and how ridiculously easy it is to convert one into the other.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 1
Reputation: shavez_israr is an unknown quantity at this point 
Solved Threads: 0
shavez_israr shavez_israr is offline Offline
Newbie Poster

Re: Print Prime Numbers(using for loop)

 
0
  #8
Apr 25th, 2006
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)
  1. int flag=1;
  2. for(int i=2;i<500;i++) //since 1 and 500 are not prime
  3. {
  4. flag=1;
  5. for(int j=2;j<i/2;j++) //since a no. cannot be divisible by a
  6. { //no. greater than its half.
  7. if(i%j==0) //if i is divisible by any no. between 2 & i/2
  8. {
  9. flag=0;
  10. break;
  11. }
  12. }
  13. if(flag==1)
  14. printf("\n",i);
  15. }
Last edited by Dave Sinkula; Apr 25th, 2006 at 10:50 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Print Prime Numbers(using for loop)

 
0
  #9
Apr 25th, 2006
> for(int j=2;j<i/2;j++) //since a no. cannot be divisible by a
> { //no. greater than its half.
It's square root of actually, but hey what the heck.

At least it's not the only mistake in your code.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Print Prime Numbers(using for loop)

 
0
  #10
Apr 25th, 2006
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?

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. static const int MAX = 500;
  6. static const int MIN = 0;
  7. static const int MAXCOL = 10;
  8.  
  9. bool isPrime(int r);
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13. int col = 0;
  14. int n;
  15.  
  16. for(n=MIN; n<=MAX; n++)
  17. if(isPrime(n)){
  18. cout << n << '\t';
  19. col++;
  20. if(col == MAXCOL){
  21. cout << '\n';
  22. col = 0;
  23. }
  24. }
  25.  
  26. system("PAUSE");
  27. return 0;
  28.  
  29. }
  30.  
  31. bool isPrime(int n)
  32. {
  33. if((n == 0)||(n == 1))
  34. return false;
  35.  
  36. int factors = 2;
  37.  
  38. for(int i=2; i<=((int)n/2); i++){
  39. if(n%i == 0){
  40. factors++;
  41. break;
  42. }
  43. }
  44.  
  45. if(factors == 2)
  46. return true;
  47.  
  48. return false;
  49. }
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC