How to write a program which sums the digits of a 4-digit number?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jordan218
    New Member
    • Mar 2010
    • 4

    How to write a program which sums the digits of a 4-digit number?

    Hi. How do I write a program that sums the digits of a 4-digit number? I have a program that works, but you have to input the digits separately. Can anyone help or possibly explain what I'm doing wrong? My professor told us as a hint to use the modulus sign (%), but i don't know how I would use that here. My code so far is below:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
    	int iA = 0;
    	int iB = 0;
    	int iC = 0;
    	int iD = 0;
    	int iTotal = 0;
    
    	cout << "Please enter a four-digit number." << endl;
    	cin >> iA >> iB >> iC >> iD;
    
    	iTotal = iA + iB + iC + iD;
    	cout << "The sum of the digits is " << iTotal << endl;
    
    	return 1;
    }
    Last edited by Banfa; Mar 11 '10, 07:37 PM. Reason: Added [code] ... [/code] tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Do you know what the modulous operator does? How would it help you isolate the digits in a four digit number?

    By the way that is not the only operator you will need you will need 1 or more of the normal arithmetic operators too.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      One of the problems you will have is that you are reading in ints, which will read in the whole 4 digit number, not just each character. If you want to do it the way you are, you'd have to read it character by character, and subtract '0' from each digit to get the numeric value.

      Unfortunately, this is a more precarious method, as a negative sign or a space in front will not be handled by default.

      Oddly enough there was a similar post in the Java forum, which might help in terms of logic.

      Comment

      • mshmyob
        Recognized Expert Contributor
        • Jan 2008
        • 903

        #4
        I won't do it for you but the basics are:

        get the mod 10 of each digit and add each together.

        cheers,

        Comment

        • Jordan218
          New Member
          • Mar 2010
          • 4

          #5
          I know the modulus operator finds the remainder when you divide one number by another, but I don't really understand what that has to do with adding the digits. And what is mod 10?

          Comment

          • mshmyob
            Recognized Expert Contributor
            • Jan 2008
            • 903

            #6
            Think about it logically:

            Say you have a number for example 4679

            If you divide the number by 10 and keep the remainder you get (4679/10=9) 9.
            You then take the number (4679) again and divide by 10 and drop the remainder your will get 467.

            Repeat the cycle until no digits are left and keep adding the remainders together and you get the sum of all the digits in the number. This can be done in a simple loop until statement.

            I'll let you figure out what the MOD (%) function gives you.

            cheers,

            Comment

            • Jordan218
              New Member
              • Mar 2010
              • 4

              #7
              I got it to work. I don't know what a loop statement is, because we just learned operators, but this works so it should be acceptable. not pretty, but it gets the job done. What do you think?

              <Code Snipped>


              Thank you so much for all your help!
              Last edited by Banfa; Mar 12 '10, 01:04 AM. Reason: Removed full code solution

              Comment

              • mshmyob
                Recognized Expert Contributor
                • Jan 2008
                • 903

                #8
                Like you said - it's not pretty but it works. And working code is the best thing you can have.

                Congrats,. When you learn about looping try changing the code down to just a few lines.

                cheers,

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Jordan218 , we do not allow the posting of full code solutions to homework questions on this forum.

                  The reasons are
                  • Preventing this becoming a forum where people come to get their homework done form them, not that you are in this category, but we do get plenty of requests for "someone give me the codez for ..." and we firmly believe that you tend to learn more by doing your own work, with a few pointers. Not to mention that a student getting their work done by someone else is basically cheating.
                  • Preventing your tutors accusing you of copying your solution from the internet, remember they can easily find us but have no hard proof that you are the poster of this question. You could end up being accused of copying your own work.
                  • Preventing anyone else on your (or similar) courses finding and using your solution.


                  I am glad you found your solution but I have had to remove it to comply with our posting guidelines. Now might be a good time for you to look them over.

                  Comment

                  • Jordan218
                    New Member
                    • Mar 2010
                    • 4

                    #10
                    My apologies. It was just a practice problem, but like you said, someone else might use it as a homework solution. I did not realize that until just now. I'll look over the guidelines to make sure it doesn't happen again. Thank you again!

                    Comment

                    • Scanny

                      #11
                      Agh, I just got pretty much the same assignment with no info aside from a vague hint from the prof and was totally lost on it. Thanks everyone here for helping me to get the idea. I felt like I was trying to solve a riddle. I hate riddles.

                      Comment

                      • arunkthaomas
                        New Member
                        • Oct 2010
                        • 3

                        #12
                        accept the 4 digit number and take mod10 it will give the last digit of ur 4digit number u please store it in array or some variable .again take the mod 10 and it will give the third digit of your 4 digit no and store it some where. continue the process

                        Comment

                        Working...