arrays

how do u write a program which is made up of the first ten prime numbers in an array called prime
This is the syntax for arrays type name [ number of elements ] = { element1, ement2... };
the number of the elements can be deduced from the number of values in the braces:
type name [ ] = { element1, ement2, element3 }; // number of elements deduced to be 3
Do you mean that the program you must write has to determine:-
a)the first ten prime numbers
and
b)insert them into an array named prime[9]
If 'yes' do you know how to determine when a number is prime?
if 'yes' do you know how to load an array with each prime number as it is detemined?
If you answered 'yes' twice you`ve cracked it.
If not post your code and question/s
1
2
3
4
5
6
7
8
9
10
11
unsigned short primes[10];
int i, j, count = 0;
for(i=2; count<10; i++){//Work until we find 10 primes
 for(j=2; j<i; j++){//Check if prime
  if(i%j!=0)//If mod returns 0 than the number is NOT a prime
   break;
 }
 if(j!=i)//check if the above loop broke or finished
  continue;//If it broke than skip this number
 primes[count] = i;//Else, we've found a prime number, insert it  
}


Yeah, I'm bored
Topic archived. No new replies allowed.