# Prime Numbers and Array...

This is a discussion on Prime Numbers and Array... within the C Programming forums, part of the General Programming Boards category; Code: #include"stdafx.h" #include<stdlib.h> #include<math.h> int main() { int int_array[10]; int i, product = 1, sum = 0; for (i = ...

1. ## Prime Numbers and Array...

Code:
```#include"stdafx.h"
#include<stdlib.h>
#include<math.h>

int main() {

int int_array[10];
int i, product = 1, sum = 0;

for (i = 0; i < 10; i++) {
int_array[i] = (rand() * 251)/RAND_MAX;
printf("int_array[%d] has a value of %d.\n", i, int_array[i]);
}

for (i = 0; i < 10; i++) {
sum = sum + int_array[i];
}
printf("The sum of the 10 integers is %d\n");

for (i = 0; i < 10; i++) {
product = product * int_array[i];
}
printf("The product of the 10 integers is %d\n");

printf("The numbers that are divisible by two are\n");
for (i = 0; i < 11; i++) {
if ((int_array[i]%2)==0)
printf ("%d\n", int_array[i]);
}

printf("The numbers that are divisible by three are\n");
for (i = 0; i < 11; i++) {
if ((int_array[i]%3)==0)
printf ("%d\n", int_array[i]);
}

printf("The numbers that are prime are\n");
for (i = 0; i < 11; i++) {
int x = sqrt (int_array[i]);
for
//divide it by 2 - x, if the remainder is ever 0, its not prime.

}```
How would I do the prime number section? I'm totally lost. :S

2. What part specificly are you having problems with? We're not here to simply hand out code. You might want to consider searching the forums. There are numerous posts on finding prime numbers if that's what you're stuck on.

Quzah.

3. I dont understand y people cannot help others who had problems. this is discussion forum anyway.. help people as much as u can, until u can. Anyway Deux, here's sample how to determine the prime number. maybe it is not efficient, but at least you have basic idea how to calculate the prime number.

Code:
```for(i=0; i<10; i++) {
printf("int_array[%d] = %d is ", i, int_array[i]);
if(!prime(int_array[i]))
printf("prime number\n");
else
printf("not prime number\n");
}
:
:
:

int prime(int a) {
int i;
for(i=2; i<a; i++) {
if(a%i == 0)
return 1;
}
return 0;
}```
rgds,

Triple_X, MY

4. I don't understand why people complain about us not just handing out code and then post their own horrible code.

5. y dont u post yur excellent code then... ;-)

6. Because like quzah I don't know exactly what they want.

7. to Thantos: seems like you are an expert in this forum. Can u help me out. You can read my post here. Any suggestion? Thanks ;-)

8. Calculating whether a number is prime is simple really. Just a few things to keep in mind.

Given the number in question is n and then value to check against the modulus is m.

Simply assume you already know 1 and 2 are prime. This way you can begin the loop which checks the modulus of n % m == 0 with 3 as the initial value of m. In this manner, you can increment m by 2 since no even numbers other than 2 are prime. This results in fewer calculations to be performed.

Also, m never needs to be checked any higher than the square root of n. Any calculations where m is greater than the squareroot of n are just redundant.

9. If I'm looking at it right, isn't there a problem in the for loop with i < 10? That would only fill the array 9 times instead of 10?

10. No. It is starting at zero, just like array indexes. Funny, that. At any rate, count from zero to nine on your fingers and get back to me.

Quzah.

11. Really didnt need to be a smartass about it. So then my next question is, why use i<11 then?

12. My little program for getting primes (enjoy):
Code:
```#include <stdio.h>
#include <stdlib.h>

typedef struct OOOOO0OOO0OO{
struct OOOOO0OOO0OO *OO00O0O0O0O0;
struct OOOOO0OOO0OO *O0O0O0O0O0O0;
int O00O0O0O00O0;
} OOOOO0OOO0OO;

OOOOO0OOO0OO* OO0O0O0O000O (OOOOO0OOO0OO *OXOO0O00O0O0, int O00O0O0O00O0);
void O000000000OO (OOOOO0OOO0OO *O0X0000OOO0O, char O0O0O00O0O0O);
void OOOOOOOOOO00(OOOOO0OOO0OO *O0X0000OOO0O);

int O00O00O0O0O0(int OOOOOOOO0000, OOOOO0OOO0OO *OOO0OO0OO0OO);

int main(void)
{
OOOOO0OOO0OO *O0X0000OOO0O = NULL, *OXOO0O00O0O0 = NULL;
int Ox0OO00O00O0=0;
OXOO0O00O0O0 = OO0O0O0O000O(OXOO0O00O0O0, 2);
O0X0000OOO0O = OXOO0O00O0O0;
OXOO0O00O0O0 = OO0O0O0O000O(OXOO0O00O0O0, 3);

for (Ox0OO00O00O0=4; Ox0OO00O00O0 < 10000; Ox0OO00O00O0++)
if ( O00O00O0O0O0(Ox0OO00O00O0, O0X0000OOO0O) )
OXOO0O00O0O0=OO0O0O0O000O(OXOO0O00O0O0, Ox0OO00O00O0);

O000000000OO(O0X0000OOO0O, '\n');
OOOOOOOOOO00(O0X0000OOO0O);
return 0;
}

OOOOO0OOO0OO* OO0O0O0O000O(OOOOO0OOO0OO *OXOO0O00O0O0, int O00O0O0O00O0)
{
OOOOO0OOO0OO *O0000OOOOO00 = malloc(sizeof(OOOOO0OOO0OO));
if ( O0000OOOOO00 == NULL )
return NULL;

O0000OOOOO00->O00O0O0O00O0 = O00O0O0O00O0;
if ( OXOO0O00O0O0 != NULL)
{
OXOO0O00O0O0->O0O0O0O0O0O0 = O0000OOOOO00;
O0000OOOOO00->OO00O0O0O0O0 = OXOO0O00O0O0;
}
else
{
O0000OOOOO00->OO00O0O0O0O0 = NULL;
}
O0000OOOOO00->O0O0O0O0O0O0 = NULL;

return O0000OOOOO00;
}

void O000000000OO(OOOOO0OOO0OO *O0X0000OOO0O, char O0O0O00O0O0O)
{
while ( O0X0000OOO0O != NULL )
{
printf("%d%c", O0X0000OOO0O->O00O0O0O00O0, O0O0O00O0O0O);
O0X0000OOO0O=O0X0000OOO0O->O0O0O0O0O0O0;
}
}

void OOOOOOOOOO00(OOOOO0OOO0OO *O0X0000OOO0O)
{
while ( O0X0000OOO0O != NULL )
{
OOOOO0OOO0OO *O0000OOOOO00 = O0X0000OOO0O;
O0X0000OOO0O = O0X0000OOO0O->O0O0O0O0O0O0;
free(O0000OOOOO00);
}
}

int O00O00O0O0O0(int OOOOOOOO0000, OOOOO0OOO0OO *OOO0OO0OO0OO)
{
while ( OOO0OO0OO0OO != NULL && (OOO0OO0OO0OO->O00O0O0O00O0 * OOO0OO0OO0OO->O00O0O0O00O0) <= OOOOOOOO0000)
if ( OOOOOOOO0000 % OOO0OO0OO0OO->O00O0O0O00O0 == 0 )
return 0;
else
OOO0OO0OO0OO = OOO0OO0OO0OO->O0O0O0O0O0O0;

return 1;
}```

13. Closed - the OP has never returned, and all semblance of "on topic" is gone.