-7

// Question 2 //Prompt the user for a four digit number (e.g. 5297) and display the sum of all of the digits (e.g. 23). //The four digit number must be read as a single number and the individual digits must be broken out using div (/) and mod (%).

            //Declare Variables
            Int32 number;

            Console.WriteLine("\n Sum of digits - please enter a four digit number: ");
            number = Int32.Parse(Console.ReadLine());
            Console.WriteLine("The first digit is {0}", number / 1000);
            Console.WriteLine("The second digit is {0}", number % 1000 / 100);
            Console.WriteLine("The third digit is {0}", number % 100 / 10);
            Console.WriteLine("The fourth digit is {0}", number % 10 / 1);



            Console.ReadLine();
Shahze123
  • 43
  • 1
  • 10
  • 1
    This is homework... while we are happy to help you with your issue, your grade is based on your personal effort and not our collective effort. What part of your code is not doing what you want it to do? – Az Za Sep 11 '13 at 20:20
  • 1
    You've got the four digits, now put them into a variable and add them together. – dcaswell Sep 11 '13 at 20:20
  • It looks like you already broke it down into the digits. Do you not know how to add numbers together? – Raymond Chen Sep 11 '13 at 20:21
  • possible duplicate of [Sum of number using user entered 4 digit numbers](http://stackoverflow.com/questions/18733619/sum-of-number-using-user-entered-4-digit-numbers) – I4V Sep 11 '13 at 20:26
  • Make sure you ask about and fully understand whichever code you put in your homework, or the test is going to be very hard. – bland Sep 11 '13 at 20:30
  • I've got a understanding of how this would work and on my previous question i asked without the code and figured most of it out just this last step on this question isn't working i keep getting the original 4 digit number and not the sum of – Shahze123 Sep 11 '13 at 22:25

1 Answers1

3

It would be simple for you to code using this logic,

  1. Declare and initalize an int variable sum =0;
  2. Loop through while the num is not equal to zero.
  3. While in loop, just add the the last digit of the number to the sum.
  4. Since you have got the last digit of the number you deduce a new num by eliminating the last position.

Hope this helps.

JNL
  • 4,683
  • 18
  • 29
  • See why people didn't post a code to this simple qustion http://stackoverflow.com/questions/18733619/sum-of-number-using-user-entered-4-digit-numbers – I4V Sep 11 '13 at 20:29
  • how would i incorperate this with my code? i placed it at the bottom and declared sum as a int32 but no luck, it's only my first couple days in class so not too sure how this goes – Shahze123 Sep 11 '13 at 20:44
  • @Shahze123 Once you get the number, just go through the 4 steps in the answer. You will get the solution in the sum variable. – JNL Sep 11 '13 at 20:46
  • I've got it all working except for the final part where i am unable to combine the 4 digits that i've broken up to get the sum of the number, i'm not exactly sure as to how i go about combining the four user inputted digits to get the sum – Shahze123 Sep 11 '13 at 22:03
  • @Shahze123 Can you paste the code in the question. We all would be more than happy to help you out. – JNL Sep 12 '13 at 01:47
  • 1
    I've got the answer it took some time to figure it out but with all the help and tips i got here i managed to get it working, thanks everyone – Shahze123 Sep 12 '13 at 18:28