944,169 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2077
  • C RSS
Nov 5th, 2009
-3

Prime numbers from 1 to 100

Expand Post »
Please, I need help in this little problem of printing all the prime numbers from to 100. Please, if you have more than one method to the question, I would really appreciate it. I also need it as soon as possible. Thanks.
Reputation Points: 4
Solved Threads: 2
Junior Poster in Training
imolorhe is offline Offline
51 posts
since Nov 2009
Nov 6th, 2009
-1
Re: Prime numbers from 1 to 100
Well, you can start off with the first two: 2, and 3, and store them in an array. From there, think of the conditions for a number to be prime and implement it, keeping in mind to compare each successive value against the prime array.

Here's hoping it helps.
Reputation Points: 34
Solved Threads: 16
Junior Poster
Chilton is offline Offline
106 posts
since Oct 2009
Nov 6th, 2009
-2
Re: Prime numbers from 1 to 100
  1. #include <math.h>
  2. #include <stdio.h>
  3.  
  4. int main(void){
  5. int i, j, isPrime;
  6. for(i = 2 ; i <= 100 ; i++){
  7. isPrime = 1;
  8. for(j = 2 ; j <= i ; j++) {
  9. if( i == j)
  10. continue;
  11. else if( i % j == 0)
  12. isPrime = 0;
  13. }
  14. if(isPrime)
  15. printf("%d, ", i);
  16. }
  17. printf("\n");
  18. return 0;
  19. }

hope this helps...
Last edited by mruthyunjaya; Nov 6th, 2009 at 7:58 am. Reason: wrong variable name
Reputation Points: 9
Solved Threads: 0
Newbie Poster
mruthyunjaya is offline Offline
5 posts
since Nov 2009
Nov 6th, 2009
-2
Re: Prime numbers from 1 to 100
#include<stdio.h>
main()
{
int i,j,flag=0;
for(i=1:i<=100;i++)
{
flag=0;
for(j=2;j<=(i/2);j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==1)
printf("%d\t",i);
}
}
Reputation Points: 9
Solved Threads: 0
Newbie Poster
vivek01anand is offline Offline
6 posts
since Oct 2009
Nov 6th, 2009
1
Re: Prime numbers from 1 to 100
And here I thought the point wasn't to simply give the answer.
Reputation Points: 34
Solved Threads: 16
Junior Poster
Chilton is offline Offline
106 posts
since Oct 2009

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: non preemptive scheduler
Next Thread in C Forum Timeline: No such file or directory





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


Follow us on Twitter


© 2011 DaniWeb® LLC