C# Program to find sum of digits of a 5 digit number
In this tutorial, we calculate digits of a number given by user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class Program { /* Function to calculate digits of a number */ static int SumDigit(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } return sum; } // Main Program public static void Main() { Console.Write("Enter Your Number:"); int n =Convert.ToInt32(Console.ReadLine()); Console.Write("The digit sum of a given number:" + SumDigit(n)); Console.ReadLine(); } } |
Output: for example, the user enters 12345 (there are 5 digits). C# Program to find sum of digits of a 5 digit number