# help with prime numbers!

This is a discussion on help with prime numbers! within the C Programming forums, part of the General Programming Boards category; Hi, I'm starting to learn c programming and this program just won't work. it is a program that calculates all ...

1. ## help with prime numbers!

Hi, I'm starting to learn c programming and this program just won't work.
it is a program that calculates all the prime numbers in the interval from 0 to n where n is the number the user enters.

here's the code

Code:
```/* Programa que calcula numeros primos */

#include <stdio.h>
#include <stdlib.h>

int numero, divisor, cont, primo;
int main(){

printf( "¿Hasta que numero desea calcular los numeros primos?\n" );
printf( "Introduzca el valor: " );
scanf("%d", &numero);
printf( "1, 2, ");
for (cont=2;cont<=numero;cont++){ // ciclo que imprime numeros primos
primo=1;
for (divisor=2; divisor<numero; divisor++){ //ciclo que revisa cuales son los numeros primos
if (divisor%numero){ // segunda condicion de salida
}else{
primo=0;
break;
}
if (primo==1){
printf("%d", cont);}
}
}```

2. How does it not work?

3. Add the missing brace; so, the code can compile.
By brace I mean this.
Code:
`}`
Then state the output from the program (also state input)
State the output expected.
Mention how the output is wrong.

Note: I suggest changing line 22 to this
Code:
`printf(", %d", cont);`
Tim S.

4. Your loop is iterating way past the point where it ought to be, many times.

If I asked you to, by hand, write down all the prime numbers up to a limit, how would you approach that? Follow the thought process step by step. Write it down on paper if that helps (believe me, it does).

You might say, "Write down a number" (let's say 3 to start). You then want to divide it by all numbers starting from 2 to the number you wrote down (3 in this example). You did this division in your code with the modulus (%) operator. But you never want your "divisor" to be greater than the value you're testing. Therefore, the "divisor" should be compared against the numerator ("cont") and not the user input ("number").

This won't fix all of the problems with the code, but that's the most prominent thing I saw while tweaking it to work right.

5. One is not prime.

6. Originally Posted by whiteflags
One is not prime.
It is not composite either

7. Originally Posted by grumpy
It is not composite either
At least it is unity

8. ok guys thank you very much, it finally worked, and yes it was missing one brace, and also the comparisons between variables where incorrect, now a simple question i suppose. why would the compiler give the following warning message:
format '%d' expects type 'int', but argument 2 has type 'int *'

if the variables are declared the same way they are here. (this is a different code) i'll post it

Code:
```// Programa que calcula n numeros primos donde n es introducida por el usuario

#include <stdio.h>
#include <stdlib.h>

int main(){
int n, divisor, cont, contnum, contpr, primo;
printf("¿Cuantos numeros primos quiere calcular?\n");
scanf("%d", &n);
printf("1");
contpr=1;
for (contnum=2;;contnum++){
primo=1;
for (divisor=2;divisor<contnum; divisor++){
if(contnum%divisor==0){
primo=0;
break;}
}
if (contpr>n){
break;}
else{
if (primo==1){
printf(", %d", &contnum);
contpr++;}
}
}
}```
it says that the error is on line 24.
thanks!

9. Did you get that error the first time around? If not, look at line 24 of your recent code and compare it with the line you had in your original code. I think you'll be able to spot the problem.

10. thanks, it worked.