-2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WholeNumbers1
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;

            int Threedigitnumber;


            Console.WriteLine("Enter in three whole numbers");
            Threedigitnumber = int.Parse(Console.ReadLine());

            sum = sum + (Threedigitnumber % 10);    // Add third digit to sum
            sum = sum + (Threedigitnumber / 10) % 10; // Add seconed digit to sum
            sum = sum + (Threedigitnumber / 100); // Add first digit to sum

            Console.WriteLine("{0} -> {1}", Threedigitnumber, sum);
            Console.ReadKey();
        }
    }
}

Can someone please explain to me how and why this code works to calculate the sum of three numbers? Im confused on how exactly the % works and why division is needed here? How does this work?

TDOT L
  • 45
  • 2
  • 10
  • Modulus returns 'one side', (integer) division the 'other side'. Write down the values after each individual operation to see what is occurring. – user2864740 Sep 22 '15 at 05:41
  • what do you mean "side?" – TDOT L Sep 22 '15 at 05:42
  • Write down the values after each individual operation to see what is occurring. What is `t % 10`? What is `t / 10`? What is `(t / 10) % 10`? What is `t / 100`? – user2864740 Sep 22 '15 at 05:42
  • how do you calculate % – TDOT L Sep 22 '15 at 05:45
  • http://stackoverflow.com/questions/3427602/c-sharp-modulus-operator (keep in mind it varies between languages; but this is C#, and used on a positive integer typed value). also http://stackoverflow.com/questions/21759390/purpose-of-using-modulus-to-find-a-numbers-c-sharp?rq=1 and https://www.linqpad.net/ is a nice tool to play with – user2864740 Sep 22 '15 at 05:46
  • thanks a ton . I think i get it now. – TDOT L Sep 22 '15 at 05:47

3 Answers3

2

What you are trying to do here is calculating the sum of digits.

when you input lets say is 123

1) sum = sum + (Threedigitnumber % 10);

(Threedigitnumber % 10); returns remainder i.e when you divide 123/10 the remainder is 3 .so your sum at this point is 3

2) sum = sum + (Threedigitnumber / 10) % 10;

(Threedigitnumber / 10) returns the quotient i.e when you divide 123/10 the quotient is 12 and when you call mod (%) again it returns remainder as 2 (12%10=2) and then add it to the sum which now become 3+2=5;

3) sum = sum + (Threedigitnumber / 100);

(Threedigitnumber / 100) returns the quotient i.e when you divide 123 by 100 it outputs 1. After adding 1 to your sum your sum now becomes 3+2+1=6

This is a terrible way to calculate the sum of the digits.An more efficient way would be

        int num, sum = 0, r;
        Console.WriteLine("Enter a Number : ");
        num = int.Parse(Console.ReadLine());
        while (num != 0)
        {
            r = num % 10;
            num = num / 10;
            sum = sum + r;
        }
        Console.WriteLine("Sum of Digits of the Number : "+sum);
        Console.ReadLine();
Rohit
  • 10,056
  • 7
  • 50
  • 82
1

Modulus means remainder after a division. In this case it is used to isolate the digits in the one's place and the ten's place. Anything mod 10 will give you the digit in the one's place. It's the remainder after dividing by 10. So 175 / 10 = 17, while 175 % 10 = 5. To get the tens place, they divide by 10 first, which division drops the one's place altogether (since this is integer arithmetic).

Steven Hansen
  • 3,189
  • 2
  • 16
  • 12
1

Suppose you introduce 123.

Step 1: 123 % 10 = 3; 
        Sum = 3
Step 2: 123 / 10 = 12;
        12 % 10 = 2;
        Sum = 5
Step 3: 123 / 100 = 1;
        Sum = 6;
tomab
  • 2,061
  • 5
  • 27
  • 38