C# Sharp Exercises: Compute the sum of the digits of an integer
C# Sharp Basic: Exercise-27 with Solution
Write a C# program and compute the sum of the digits of an integer.
Sample Solution:-
C# Sharp Code:
using System;
public class Exercise27 {
public static void Main() {
Console.Write("Input a number(integer): ");
int n = Convert.ToInt32(Console.ReadLine());
int sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
Console.WriteLine("Sum of the digits of the said integer: " + sum);
}
}
Sample Output:
Input a number(integer): 12 Sum of the digits of the said integer: 3
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# program to compute the sum of the first 500 prime numbers.
Next: Write a C# program to reverse the words of a sentence.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework