944,138 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 61667
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 22nd, 2006
-1

Print Prime Numbers(using for loop)

Expand Post »
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)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
john_hasan is offline Offline
5 posts
since Apr 2006
Apr 22nd, 2006
0

Re: Print Prime Numbers(using for loop)

No problem, just show us what you manage to do so far...
Reputation Points: 55
Solved Threads: 6
Junior Poster
Micko is offline Offline
148 posts
since Aug 2005
Apr 22nd, 2006
-2

Re: Print Prime Numbers(using for loop)

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
john_hasan is offline Offline
5 posts
since Apr 2006
Apr 22nd, 2006
0

Re: Print Prime Numbers(using for loop)

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. }
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Apr 22nd, 2006
0

Re: Print Prime Numbers(using for loop)

> 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. }
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 22nd, 2006
0

Re: Print Prime Numbers(using for loop)

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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Apr 22nd, 2006
0

Re: Print Prime Numbers(using for loop)

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 25th, 2006
0

Re: Print Prime Numbers(using for loop)

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shavez_israr is offline Offline
1 posts
since Apr 2006
Apr 25th, 2006
0

Re: Print Prime Numbers(using for loop)

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 25th, 2006
0

Re: Print Prime Numbers(using for loop)

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. }
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: string manipulation
Next Thread in C Forum Timeline: How to solve linker error at DEVC++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC