[Solved] remainder (%) operator c#


Check Output you can understand every steps and what’s happening in every step

using System;
class MainClass {
  public static void Main (string[] args) {
    Console.WriteLine("Please enter a number: ");
    int number = Convert.ToInt32(Console.ReadLine());
    int sum = 0;
    while (number> 0)
    {
      Console.WriteLine("Steps");
      Console.WriteLine("--------------");
      Console.WriteLine("Digits: "+ (number % 10));
      sum = sum + (number % 10);
       Console.WriteLine("sum: "+ sum);
      number = number / 10;
      Console.WriteLine("number after number/10: "+ (number).ToString());
      Console.WriteLine("--------------");
    }
Console.WriteLine("{0}{1}", "Sum of digits is: ", sum.ToString());
  }
}

Output:

Please enter a number: 
 9874
Steps
--------------
Digits: 4
sum: 4
number after number/10: 987
--------------
Steps
--------------
Digits: 7
sum: 11
number after number/10: 98
--------------
Steps
--------------
Digits: 8
sum: 19
number after number/10: 9
--------------
Steps
--------------
Digits: 9
sum: 28
number after number/10: 0
--------------
Sum of digits is: 28

2

solved remainder (%) operator c#