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();
}
}
}