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. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    4

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,581
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,374
    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. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    One is not prime.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Quote Originally Posted by whiteflags View Post
    One is not prime.
    It is not composite either
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Quote Originally Posted by grumpy
    It is not composite either
    At least it is unity
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    4
    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");
        printf("Ingrese la cantidad: ");
        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. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,374
    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. #10
    Registered User
    Join Date
    Jun 2012
    Posts
    4
    thanks, it worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  2. non prime numbers or composite numbers program.help plz!
    By danishzaidi in forum C Programming
    Replies: 10
    Last Post: 11-15-2011, 10:10 AM
  3. Help with prime numbers
    By Clean Killa in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2002, 03:42 PM
  4. prime numbers
    By Unregistered in forum C Programming
    Replies: 17
    Last Post: 08-20-2002, 08:57 PM
  5. Prime Numbers
    By Korn1699 in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2001, 08:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21