Thread: Simple division program help

  1. #16
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well the 'digit' depends on the number base, but I'm assuming you mean the decimal representation? In that case, you can extract the digit (D) by obtaining the modulo of the number (N) with 10. Run the even/odd test on D, increment the appropriate counters, and then divide N by 10 and repeat the process, until N is zero.

  2. #17
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    int XXX;
    int aa;
    int bb;
    int cc;

    printf( "Please enter a number: " );
    scanf( "%d", &XXX );

    aa = XXX / 100;
    bb = XXX / 10 - aa * 10;
    cc = XXX - aa * 100 - bb * 10;
    Okay i seem to have succeded in isolating each digit even though I'm sure there's a simpler way than what i did, now i'm going to work on adding them up in the right way.

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First of all, variable names like 'XXX' and 'aa' are not very informative. The more descriptive, the less comments you have to write to explain what each variable does. Anyway, you need a loop. Here's some pseudocode:

    Code:
    
    
    Read input into VALUE
    Loop:
        Set DIGIT to VALUE modulo BASE.
        Test DIGIT for evenness. If so, increment EVEN counter, otherwise ODD.
        Divide VALUE by base. If the result is zero, break out of loop.

  4. #19
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    I'm not good enough at c to even understand what you mean.

    Code:
    #include <stdio.h>
    
    int main()
    {
      int XXX;
      int aa;
      int bb;
      int cc;
    
    
    
    
      printf( "Please enter a number: " );
      scanf( "%d", &XXX );
    
       aa = XXX / 100;
       bb = XXX / 10 - aa * 10;
       cc = XXX - aa * 100 - bb * 10;
    
       if (aa % 2 == 0 && bb % 2 == 0 && cc % 2 == 1 && aa + bb - cc == 0);
           printf ("yes");
    
       else if (aa % 2 == 0 && bb % 2 == 1 && cc % 2 == 1 && aa - bb - cc == 0);
            printf ("yes");
    
       else if (aa % 2 == 1 && bb % 2 == 0 && cc % 2 == 1 && aa - bb + cc == 0);
          printf ("yes");
    
       else if (aa % 2 == 1 && bb % 2 == 1 && cc % 2 == 0 && aa + bb - cc == 0);
           printf ("yes");
    
        else if (aa % 2 == 1 && bb % 2 == 0 && cc % 2 == 0 && aa - bb - cc == 0);
            printf ("yes");
    
        else if (aa % 2 == 0 && bb % 2 == 1 && cc % 2 == 0 && aa - bb + cc == 0);
           printf ("yes");
    
    
    
    
       else printf ("no"};
    
    
    
    
      return 0;
    }
    Anyway here's what I came up with, it's a code which answers yes when the sum of the odd digits equals the sum of the even digits in a number (in this code 3 digit and less numbers).

    My problem is that I get an error saying "syntax eror before else", please help me.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have ; at the end of all your if checks, which effectively makes them do nothing.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Thanks it's up and working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  2. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  3. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM