Thread: help with prime numbers!

  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
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

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

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by grumpy
    It is not composite either
    At least it is unity
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    4,513
    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, 11:10 AM
  3. Help with prime numbers
    By Clean Killa in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2002, 04: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, 09:52 PM