1,105,447 Community Members

how do u find prime numbers in an array

Member Avatar
srinath33
Newbie Poster
3 posts since Feb 2007
Reputation Points: -1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hlp please

Member Avatar
Ancient Dragon
Achieved Level 70
27,632 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,037 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

if you search these threads you will find several threads on this topic.

Member Avatar
nadith_cs
Newbie Poster
13 posts since Feb 2007
Reputation Points: 1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

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....

Member Avatar
srinath33
Newbie Poster
3 posts since Feb 2007
Reputation Points: -1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

dude we need2 accept the numbers in an array.... so among those nos wi nid 2 find

Member Avatar
John A
Vampirical Lurker
5,285 posts since Apr 2006
Reputation Points: 1,896 [?]
Q&As Helped to Solve: 408 [?]
Skill Endorsements: 12 [?]
Team Colleague
 
0
 
Member Avatar
Lazaro Claiborn
Junior Poster
168 posts since Jan 2007
Reputation Points: 1 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
jwenting
duckman
8,359 posts since Nov 2004
Reputation Points: 1,399 [?]
Q&As Helped to Solve: 447 [?]
Skill Endorsements: 35 [?]
Infraction Points: 5
Team Colleague
 
0
 

I don't know how "u" does it. I do know how I'd do it.

Member Avatar
goutham_see
Newbie Poster
7 posts since Feb 2007
Reputation Points: 1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
/* This program is for checking a single integer*/
#include<stdio.h>
#include<stdlib.h>
main()
{
 int a,i;
 printf("\nEnter a positive integer\n");
 scanf("%d",&a);
 if(a<=0)
 {
  printf("\nInvalid input\n");
  exit(1);
 }
 for(i=2;i<=a-1;i++)
   if(a%i==0)break;
 if(i==a)printf("\n%d is prime\n",a);
 else printf("\n%d is not a prime\n",a);
}


/* This is for elements in an array*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
main()
{
 int a[MAX],i,m;
 printf("\nEnter elements into array\n");
 for(m=0;m<=MAX-1;m++)
 {
    scanf("%d",&a[m]);
    if(a[m]>0)
    {
          for(i=2;i<=a[m]-1;i++) if(a[m]%i==0)break;
          if(i==a[m])printf("\n%d is prime\n",a[m]);
          else printf("\n%d is not a prime\n",a[m]);
    }
    else printf("\n%d is not positive\n");
 }
}
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article