x%10 then what? [getting digits sum] of a (positive long int)

This is a discussion on x%10 then what? [getting digits sum] of a (positive long int) within the C++ Programming forums, part of the General Programming Boards category; If I have a (long int x) that I want to get the sum of its digits, I know I ...

  1. #1
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Question x%10 then what? [getting digits sum] of a (positive long int)

    If I have a (long int x) that I want to get the sum of its digits, I know I should use modulus & devision, declare a temp variable as well as one for sum. But I am really confused about the exact equations, I only know how to get the first digit (units) by using:

    Code:
    temp = x % 10;
    sum += temp;
    and I think that I will use loops as well, do I have to?
    and what are the equations/code used?

    Thanks,
    Amy
    It ain't illegal until you get caught..

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, think of what the modulo operator does - it returns the remainder of a division. so, if you do x%10, you're dividing by 10 and returning what's left (a single digit). what happens when you use 100? 1000?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, you've got the first digit; now if you want to keep using the same formula for getting the value of each digit (i.e. x % 10), you'll need to keep moving digits over to the right. You can do this by dividing x by 10 (any decimal part will automatically be cut off). For example:
    Code:
    temp = x % 10;  //Get the units digit
    sum += temp;  //Add to running total
    x /= 10;  //divide x by 10, store the result in x. This shifts everything to the right one place.
    
    temp = x % 10;  //Get the new units digit
    sum += temp;  //Add to running total
    x /= 10;  //Shift everything toward the units digit one place
    
    //etc. etc. for each digit
    As you can imagine, this can very easily be put into a loop that ends when you've reached the last digit, which I'll leave to you to do.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by Hunter2
    Well, you've got the first digit; now if you want to keep using the same formula for getting the value of each digit (i.e. x % 10), you'll need to keep moving digits over to the right. You can do this by dividing x by 10 (any decimal part will automatically be cut off). For example:
    Code:
    temp = x % 10;  //Get the units digit
    sum += temp;  //Add to running total
    x /= 10;  //divide x by 10, store the result in x. This shifts everything to the right one place.
    
    temp = x % 10;  //Get the new units digit
    sum += temp;  //Add to running total
    x /= 10;  //Shift everything toward the units digit one place
    
    //etc. etc. for each digit
    As you can imagine, this can very easily be put into a loop that ends when you've reached the last digit, which I'll leave to you to do.
    I couldn't know the condition which states that the number ended when using this code.
    It ain't illegal until you get caught..

  5. #5
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by major_small
    well, think of what the modulo operator does - it returns the remainder of a division. so, if you do x%10, you're dividing by 10 and returning what's left (a single digit). what happens when you use 100? 1000?
    I originally wanted to write a program to input a positive integer and to compute the sum of its digits, then determine whether it is divisible by 9 using the rule (an integer N is divisible by 9 if the sum of its digits is divisible by 9)..

    I made it this way and it is working:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	long int N;
    
    	cin>>N;
    
    	float temp = 0;
    	int sum = 0;
    	long int modulus;
    	long int division;
    
    	temp = N % 10;
    	sum += temp;
    
    	modulus = 100;
    	division = 10;
    
    	while(temp>=0)
    	{
    		temp = N % modulus;
    		temp /= division;
    		sum += temp;
    
    		modulus *= 10;
    		division *= 10;
    
    		temp /= modulus;
    	}
    	
    	if(sum==9)
    		cout<<"The number is divisible by 9.";
    	
    	cin.ignore();
    	cin.get();
    	return 0;
    }
    Is it a good way??
    It ain't illegal until you get caught..

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    modulus *= 10;
    division *= 10;

    temp /= modulus;

    leave those lines out. Delete the following and replace them by intializing the values at the time of declaration

    modulus = 100;
    division = 10;

    You modify temp in the while loop so leave out these as well.

    temp = N % 10;
    sum += temp;

    Before you do that however, set the value of temp to something other than 0 and run your program. Analyze the results and see why I made the suggestions.

    Oh, and I think you want

    if(sum % 9 == 0)

    instead of

    if(sum==9)

    to meet the protocol you suggested.
    You're only born perfect.

  7. #7
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by elad
    Before you do that however, set the value of temp to something other than 0 and run your program. Analyze the results and see why I made the suggestions.
    Why something other than 0?
    It ain't illegal until you get caught..

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>determine whether it is divisible by 9
    Well, the most efficient way to do so would be simply:
    Code:
    bool isDivisible = ((theNumber % 9) == 0);
    Anyhow, back to this:
    I couldn't know the condition which states that the number ended when using this code.
    You can tell that the number ended, simply when x == 0. Since you're continually getting the units digit and then truncating it, when x reaches 0 you know that you've run out of digits to sum In this way, you'll replace 10 confusing lines with 3 simple ones.
    Last edited by Hunter2; 02-07-2005 at 08:40 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    There are only 3 items you need for this to work...

    1st, a sentinel for the loop. You used while ( temp >= 0 ). However I would recommend using simply
    Code:
    while ( temp )
    This way it will also work with negative numbers (for whatever reason). As long as Temp is a non-zero value, it will result as true.

    2nd, you need to strip the digit and add it to the sum. sum += ( temp % 10 ); will do that.

    3rd, remove the digit from the number entirely. temp /= 10;

    And there you have a simple while loop with 2 statements...
    Code:
    	while ( temp )
    	{
    		sum += ( temp % 10 );
    		temp /= 10;
    	}

  10. #10
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by Hunter2
    >>determine whether it is divisible by 9
    Well, the most efficient way to do so would be simply:
    Code:
    bool isDivisible = ((theNumber % 9) == 0);
    Yes, I understand this way and I know it is the efficient, but in my program I wanted to determine that using a certain rule, which is: an integer N is divisible by 9 if the sum of its digits is divisible by 9.

    Anyhow, back to this:
    You can tell that the number ended, simply when x == 0. Since you're continually getting the units digit and then truncating it, when x reaches 0 you know that you've run out of digits to sum In this way, you'll replace 10 confusing lines with 3 simple ones.
    Oh yea, right. Because I will already keep moving digits over to the right
    Thanks alot,

    -Amy
    It ain't illegal until you get caught..

  11. #11
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Question

    Oh, there is a small problem with the program, if the input (N) exceeds 10 digits the program ends, although N is declared as a long integer.
    Does anybody know why this happens?
    It ain't illegal until you get caught..

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, the problem is simply that a long integer cannot hold a number that large. You can look the exact boundaries up in a book or on the internet pretty much anywhere (C++ datatype sizes). If you want to hold a larger 'number', you'll have to input it as a string of characters (which, incidentally, makes your algorithm much more intuitive - the number is already broken up into an array of single digits).

    My guess as to why it's quitting is that you're using cin.get() to pause your program at the end. However, if you have an error inputting some data (i.e. cin >> theNumber, where the input is to large for theNumber to hold), an error flag will be set in cin and all successive input operations will return immediately with a failure. Thus, the rest of the program flashes by too quickly for you to see and the cin.get() at the end doesn't pause anything.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-13-2007, 09:55 AM
  2. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  3. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 05:28 AM

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