1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C programming prime numbers

  1. May 25, 2010 #1
    1. The problem statement, all variables and given/known data
    Hello, i want to calculate and print prime numbers from 1 to 20. Ive provided my code below, and the program compiles but its just printing all numbers from 1 to 20, why?
    also have i used the continue statement correctly, since if it is found that a number is not prime then the next iteration starts in the for loop (ie all stuff below the continue statement is discarded).
    Thanks


    3. The attempt at a solution

    #include<stdio.h>
    main () {
    int x, i=2, y;
    printf("1\n2\n");
    for(x=3;x<21;x++) {
    i=2;
    while(i<x && y!=0) {
    y=x%i;
    i=i+1;
    }

    if(y==0) continue;
    else printf("%d\n",x);
    }
    }
     
    Last edited: May 25, 2010
  2. jcsd
  3. May 25, 2010 #2
    Well you never reset i in the outer loop. In other words, i=2 the first time the inner loop runs, then i=3 and x=4, then i=4 and x=5, etc
     
  4. May 25, 2010 #3
    ok ive reset it, but its just prints 1 2 3
     
  5. May 25, 2010 #4

    DaveC426913

    User Avatar
    Gold Member

    Have you run through your code on paper? i.e. Make boxes labeled x, y, i and then walk through the code line by line? It will show you why y is not equaling 0.
     
  6. May 25, 2010 #5
    ah yes, starting when x =4 and i=2 then 4%2=2 but this should be zero according to what i want.
    Im not sure how to change this
     
  7. May 25, 2010 #6

    DaveC426913

    User Avatar
    Gold Member

    Question:

    You declare y as an int, then in your while loop you test if it's !=0. Can you do this in c? You have not assigned it a value. I guess, since it's an int, it is aujto-assigned zero?
     
  8. May 26, 2010 #7
    yes you can do that in c, also it isn't auto assigned to 0, since if it was then, it would only print 1 and 2, but its printing 1 2 and 3.

    The error is where y never becomes zero. like is said above i need to fix the condition for the while loop. eg: when x=4 and i=2 then 4%2=2 but this should be zero according to what i want.
    Im not sure how to change this.
     
  9. May 26, 2010 #8

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    The error is in this statement: while(i<x && y!=0) { ... }

    You haven't set y before entering the loop. If y is zero the loop will never be entered.

    Learn to use a debugger.
     
  10. May 26, 2010 #9
    ok so i have:

    #include<stdio.h>
    main () {
    int x, i=2, y=1;
    printf("1\n2\n");
    for(x=3;x<21;x++) {
    i=2;
    while(i<x && y!=0) {
    y=x%i;
    i=i+1;
    }

    if(y==0) continue;
    else printf("%d\n",x);
    }
    }
     
  11. May 26, 2010 #10

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    And does that do what you want? (No.)

    Don't just stab in the dark when programming (or solving problems, in general).

    Do you have a debugger? Do you know how to use it?
     
  12. May 26, 2010 #11

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Or, for that matter, you could execute this simple program by hand. Try it. Why doesn't your program print 5? (It only reports 1, 2, and 3 as primes.)

    BTW, one is not prime. It stands by itself.
     
  13. May 26, 2010 #12
    i know why it doesn't print 5 because y never becomes zero (zero remainder when when a number is a factor of another). Read the comments above before posting. I am not taking a stab in the dark, i know my problems as i have stated them and need help. If you want to post unhelpful comments then go elsewhere.

    Im a mathematician, and it is a matter of opinion whether 1 is prime or not. Some textbooks say 1 is prime, whilst others don't, depending on how you define a prime number.

    I dont want your help, ill just go elsewhere.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook