using System; class SumOfDigits { static void Main() { Console.Write("Enter a number: "); int num = Convert.ToInt32(Console.ReadLine()); int sum = 0; while (num > 0) { sum += num % 10; num /= 10; } Console.WriteLine("Sum of digits: " + sum); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter