Thread: Simple division program help

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    11

    Simple division program help (added a new question)

    My initial problem has been solved, i have another question, is it possible for me to check whether each digit of an entered number is odd or even? for example 121 was entered' how do I apply (number % 2 == 0) command to each separate digit?

    Basicaly: I want to find out if the sum of all the even digits equalls the sum of all the odd digits
    Hello, I am trying to create a program that can tell me if the number I type in can be divided by 11 without any remains.

    For example if i type in 121 it will answer "yes" while if I type in 139 it will say no.

    I have started learning c around two hours ago and have been aiming to make this program all along...unfortunately i have failed.

    If someone has the time could you please write this simple program for me and explain any complicated parts.

    Thank you for your attention,

    Andrey
    Last edited by Syle; 09-29-2009 at 04:57 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. Post your own code and explain what you're having problems with.


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

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Code:
    #include <stdio.h>
    
    int main()
    {
      int this_is_a_number;
      
      printf( "Please enter a number: " );
      scanf( "%d", &this_is_a_number );
      
      if (this_is_a_number / 11 = /* whole number/*) (printf ("yes"));
    else (printf ("no"));
    }
    unfortunately this is as far as i got' I have no idea on how to continue.
    Last edited by Syle; 09-29-2009 at 04:35 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Google modulus operator.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You can use the modulo ('%') operator to find the remainder, eg:

    121 % 10 is 1, so not evenly divisible
    121 % 11 is 0, so the number is evenly divisible by 11

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Code:
    #include <stdio.h>
    
    int main()
    {
      int this_is_a_number;
    
      printf( "Please enter a number: " );
      scanf( "%d", &this_is_a_number );
    
      if (this_is_a_number % 11 == 0) (printf ("yes"));
    else (printf ("no"));
    
      return 0;
    }
    Thank you very much quzah for not writing the code for me but rather making me write it myself and thank you very much rags_to_riches for telling me what to look for.

    Thank you sebastiani for replying.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    On your if/else, remember that { } is a new block of code / scope level, and () is more of operator.
    Code:
    if( x )
    {
        ...this...
        ...can have multiple lines...
        ...including declaring new varaibles...
    }
    else
    {
        ...same here...
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Syle View Post
    Code:
    #include <stdio.h>
    
    int main()
    {
      int this_is_a_number;
    
      printf( "Please enter a number: " );
      scanf( "%d", &this_is_a_number );
    
      if (this_is_a_number % 11 == 0) (printf ("yes"));
    else (printf ("no"));
    
      return 0;
    }
    Thank you very much quzah for not writing the code for me but rather making me write it myself and thank you very much rags_to_riches for telling me what to look for.

    Thank you sebastiani for replying.

    Just nitpicking here, but you don't need the parenthesis around the printf statement, and you should indent your code properly, eg:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int this_is_a_number;
        printf( "Please enter a number: " );
        scanf( "%d", &this_is_a_number );
        if (this_is_a_number % 11 == 0) 
            printf ("yes");
        else 
            printf ("no");
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Thank you all once again, if possible could you adress my second question?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't see a second question anywhere. What's your question?


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

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You don't need to separate the digits - if the entire number is evenly divisible by two it's, well, even.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    That is not what i want to check though, I want to find out if the sum of all the even digits equalls the sum of all the odd digits.

    Quzah I have added my second question to the initial post.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Then you need to add up all the digits where %2 == 1, and all the ones that %2 == 0 in another variable. So, that means you need two different varaibles, and to use an if check to decide which one gets incremented by whatever. Give it a shot, let us know what you get stuck on.


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

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    That is not the part I am having trouble with, I am having trouble with finding a way to adress each digit.

    int this_is_a_number;
    printf( "Please enter a number: " );
    scanf( "%d", &this_is_a_number );
    After this command how do I separate the digits of the entered number?
    Last edited by Syle; 09-29-2009 at 05:02 PM.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you split the 1s place from a number? Mod by 10.
    How do you split digit X from a number? Divide by 10 ... or 100, or 1000. Or just divide by 10 over and over.

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

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