Thread: Testing for truth(while statements)

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    3

    Testing for truth(while statements)

    Hey all, I'm a super-noob and basically I'm trying to finish off this code block for error checking and displaying multiple results from multiple checks.
    Maybe that doesn't make sense what I typed but here is a run down of my code

    user inputs a number and 'wants' to be told the factors of that number
    what is a 'factor'
    factor=division of the number inputted by another number which gives a remainder of zero
    in other words
    factor=(input/anumber==r0)
    however, all the factors of the input must be displayed
    so a test must be done for valid factors
    'if factor is true, then display anumber used in factor formula'
    'otherwise, if not true, then test the next number after the previously tested anumber until the input is reached, since every real/whole number has at least 2 factors (1 and the number itself)'


    #include <stdio.h>


    int main(void)
    {
    int factor=0;
    int input=0;
    int anumber=2;
    printf("Enter a number (1-5000):");
    scanf("%d",&input);
    factor=(input%anumber);
    if(factor>=1)
    printf("The factors for %d are 1, & %d.",input,input);

    else
    {

    while(factor==0)
    {
    printf(", %d",anumber);
    anumber=anumber+1;
    }
    }
    return 0;
    }
    I realize this is incomplete(missing loop for range test(1-5000)) but I can't quite seem to get a handle on satisfying all the problems associated with this block.

    In a nutshell, I need:
    all the factors from a given input on the same line
    if the input is 1, to simply display, The factor for 1 is 1.
    disregard all non-valid factors

    I've been cracking at this all day and bupkis.

    I will note it's school related, but not an assignment/homework.
    Any help is appreciated.
    Thank you.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Let's start with finding the remainder:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int x;
    
      printf ( "Enter a number: " );
      fflush ( stdout );
    
      if ( scanf ( "%d", &x ) == 1 ) {
        int result = x / 2;
        int remainder = x % 2;
    
        printf ( "%d / 2 = %d with %d remaining.\n", x, result, remainder );
      }
    
      return 0;
    }
    If (x % y) is 0 then you've found a factor. And if you can find one factor, I'm sure you can figure out how to find all factors from 1 to x.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Isn't using 'while(factor==0)' essentially testing for that?
    I don't mean to backlash, then again the output of modulus will be the remainder, meaning I have that already.
    For whatever reason since I've been at it so long, I can't see past(or recall) how to display the proper factors. More importantly the 'true-factors' or ones that past the 'while(factor==0)' loop being displayed.

    I'm a beyond-noob so please, if you could simplify things bit a more, I might understand.
    sorry...

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Isn't using 'while(factor==0)' essentially testing for that?
    No. Yes, it's testing the remainder, but the loop logic is completely backward. You loop through all of your numbers and test, not the other way around:
    Code:
    for n = 1 to x do
      if x mod n = 0 then
        print 'factor = ', n
      endif
    loop
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    In that case do I want to:

    while(loop!=0)

    instead...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  4. C++ bit testing
    By Vicious in forum C++ Programming
    Replies: 3
    Last Post: 09-19-2004, 11:44 AM
  5. tips for testing conditions
    By Silvercord in forum Game Programming
    Replies: 12
    Last Post: 04-10-2003, 01:42 PM