-1

I have my code get me the number of digits of an entered number. But if I dial it by hand I get another ...

Let's take 125 and - for example- modulate it by 10. Let's even put that in a while loop and let our number be divided by 10 each round. We get:

125%10 -> 5
12,5%10 -> 2,5
1,25%10 -> 1,25

Our sum would be 8,75. But if I use the code down below we get 8.

Does anyone know why it is that different?

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

namespace EvenOrOdd
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, sum = 0, r;
            Console.WriteLine("\nEnter 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();
        }
    }
}
Kalex
  • 35
  • 6
  • 1
    You're parsing the numbers into an `int` which means that the numbers get rounded off. – Hans Kilian Jun 14 '19 at 11:56
  • 4
    `12,5%10 -> 2,5` How would you store 2.5 in an `int`? – mjwills Jun 14 '19 at 11:57
  • You use an integer (int) to store your result. Integers cant display decimal places so it simply saves your 8.75 as 8. Change your int up for a float and you should get your desired result with decimal places. – Niklas7 Jun 14 '19 at 11:59
  • 1
    @elgonzo I see it now. He enters an integer number and then try to sum the remainders. Then the first two comments applies. – Steve Jun 14 '19 at 12:04
  • `Sum of Digits of the Number` Without understanding the context I would **expect** it to be 8 (1+2+5) - especially if this is homework. – mjwills Jun 14 '19 at 12:04
  • @Steve, correct. Now i see it too. The code iterates over `num`, dividing it by 10 in every iteration. Doh! I need new glasses ;-) –  Jun 14 '19 at 12:05
  • @elgonzo 125 is being entered. The other values is what he/she is expecting to occur on each loop iteration. – mjwills Jun 14 '19 at 12:05
  • As others have said, you are using integer math. Not only does it apply to the `%` modulo operation, but also to the division. If `int num = 125` then `num / 10` will evaluate to `12` (because any fraction will simply be truncated from a number in integer math) –  Jun 14 '19 at 12:08
  • Thank you everyone! It seems not only my datatype, but also my logic was bit off, thanks for resolving that~ @mjwills ... it's mostly about a little number game i tried programming where i need **integer** only, but i thought too far and false. – Kalex Jun 14 '19 at 13:00

2 Answers2

1

You should use a floating point type, like float or double. An int cant hold the number "8.75".

Just replace int with double or float and it should work.

JP-Hundhausen
  • 70
  • 1
  • 7
1

Try this code

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

namespace EvenOrOdd
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal num, sum = 0, r;
            Console.WriteLine("\nEnter a Number : ");
            num = int.Parse(Console.ReadLine());
            while (num >= 1)
            {
                r = num % 10;
                num = num / 10;
                sum = sum + r;
            }

            Console.WriteLine("Sum of Digits of the Number : " + sum);
            Console.ReadLine();
        }
    }
}

There are 2 issues here

-

  1. int num, sum = 0, r; to be changed to decimal num, sum = 0, r;, this will make the decimals to be considered
  2. This logic seems to be another issue while (num != 0), the following will be the iteration results
    • Iteration1 - 125 Mod 10 = 5
    • Iteration2 - 12.5 Mod 10 = 2.5
    • Iteration3 - 1.25 Mod 10 = 1.25
    • Iteration4 - 0.125 Mod 10 = 0.125

and goes on as 0.125 is != 0 this will continue

  • Thank you! You're totally right, my logic also was pretty off here, thanks for pointing that out! :) – Kalex Jun 14 '19 at 12:56