what do you you mean, this prog doesnt work??

This is a discussion on what do you you mean, this prog doesnt work?? within the C++ Programming forums, part of the General Programming Boards category; someone asked earlier about a program involving prime numbers, and i figured they would be pretty easy to work with, ...

  1. #1
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    what do you you mean, this prog doesnt work??

    someone asked earlier about a program involving prime numbers, and i figured they would be pretty easy to work with, so i threw together a simple program to show all of the prime numbers less than 1000. this program looks like it should run perfectly from what i see, but it seems that it comes up with no results at all.

    my program looks like this:

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

    void prime();

    int i=1000,x=0,y,z;

    void main(){

    printf("All of the prime numbers less than 1000:\n");

    while ( i > 2 ){
    z=0;
    x=i;
    prime();
    i = --i;
    }

    printf("2\n1\n");
    system ("PAUSE");
    }

    void prime (){

    while ( x != 1 ){

    y = i % (x-1);
    x = --x;

    if ( y == 0 ){
    x = 1;z = 1;}

    }

    if ( z == 0 ){
    printf("%d\n", i); }

    }

    and the output looks like this:

    All of the prime numbers less than 1000:
    2
    1
    press any key to continue...

    the prime() function is supposed to check for divisibility in every number below i. if any number divides evenly then it sets z to 1. if z never gets set to 1, and x gets all the way to 1, it prints i and then subtracts one from i and repeats the loop. why doesnt this ever print anything?

  2. #2
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    ok, i fixed what i thought was the problem (it only checks for if x = 1, but if x = 2, then it divides by x - 1 which is one, and every number is divisible by 1 so it ends the loop and doesnt print),

    BUT

    now i have another problem..... i changed the while statement to

    while ( x != 2 )

    and now i get an illegal opperation.

    *sigh*....

  3. #3
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    woo, fixed it.

    no thanks to any of you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 11:53 AM
  5. Developers Wanted
    By Quasicom in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 08-24-2005, 01:46 AM

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