943,911 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 11759
  • C RSS
Feb 18th, 2007
0

how do u find prime numbers in an array

Expand Post »
hlp please
Similar Threads
Reputation Points: 9
Solved Threads: 0
Newbie Poster
srinath33 is offline Offline
3 posts
since Feb 2007
Feb 18th, 2007
0

Re: how do u find prime numbers in an array

if you search these threads you will find several threads on this topic.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 18th, 2007
1

Re: how do u find prime numbers in an array

first write a program to catch all the prime numbers in to an array in a given or a chosen range....
and compare the elements of prime numbers with the each element of the array u want...
if one of ur array element is equal to a element of prime number array,,that means it's a prime number....

e.g.

A[4]={1,5,3,4,17}; //range is 1 to 20

primArray[10]={2,3,5,7,11,13,17,19}
now compare array 'A' with the each element of 'primArray'.

if u find hard to write a program to catch prime numbers of a given range to an array email me....
Reputation Points: 11
Solved Threads: 0
Newbie Poster
nadith_cs is offline Offline
13 posts
since Feb 2007
Feb 18th, 2007
0

Re: how do u find prime numbers in an array

dude we need2 accept the numbers in an array.... so among those nos wi nid 2 find
Reputation Points: 9
Solved Threads: 0
Newbie Poster
srinath33 is offline Offline
3 posts
since Feb 2007
Feb 18th, 2007
0

Re: how do u find prime numbers in an array

Speak English please. And once you do that, you can show some effort.
http://www.daniweb.com/techtalkforum...cement8-2.html
http://www.catb.org/~esr/faqs/smart-questions.html
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 20th, 2007
0

Re: how do u find prime numbers in an array

Click to Expand / Collapse  Quote originally posted by srinath33 ...
hlp please
A prime number is a number that can only be evenly divided by itself and 1. Let number x equal a given element in the array. Now you can for loop from 2 to x-1 and use the modulus operator. If the resulting value is equal to 0 then it is certainly not a prime.

Good luck, LamaBot
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Feb 20th, 2007
0

Re: how do u find prime numbers in an array

I don't know how "u" does it. I do know how I'd do it.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Feb 21st, 2007
0

Re: how do u find prime numbers in an array

  1. /* This program is for checking a single integer*/
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. main()
  5. {
  6. int a,i;
  7. printf("\nEnter a positive integer\n");
  8. scanf("%d",&a);
  9. if(a<=0)
  10. {
  11. printf("\nInvalid input\n");
  12. exit(1);
  13. }
  14. for(i=2;i<=a-1;i++)
  15. if(a%i==0)break;
  16. if(i==a)printf("\n%d is prime\n",a);
  17. else printf("\n%d is not a prime\n",a);
  18. }
  19.  
  20.  
  21. /* This is for elements in an array*/
  22. #include<stdio.h>
  23. #include<stdlib.h>
  24. #define MAX 5
  25. main()
  26. {
  27. int a[MAX],i,m;
  28. printf("\nEnter elements into array\n");
  29. for(m=0;m<=MAX-1;m++)
  30. {
  31. scanf("%d",&a[m]);
  32. if(a[m]>0)
  33. {
  34. for(i=2;i<=a[m]-1;i++) if(a[m]%i==0)break;
  35. if(i==a[m])printf("\n%d is prime\n",a[m]);
  36. else printf("\n%d is not a prime\n",a[m]);
  37. }
  38. else printf("\n%d is not positive\n");
  39. }
  40. }
Last edited by WaltP; Feb 21st, 2007 at 2:58 am. Reason: Added Code Tags -- please use them yourself. Instructions in the stickys.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
goutham_see is offline Offline
7 posts
since Feb 2007

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: Convert to Absolute Values
Next Thread in C Forum Timeline: read each line of file





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


Follow us on Twitter


© 2011 DaniWeb® LLC