For finding the sum of the digits of an entered number, first, we need to split the number into individual digits and then, we add those individual digits.
For example, to find the sum of 7810, first, we will divide this number into individual digits and then, we will add these individual digits i.e., 7+8+1+0. So, the sum would be 16.
First, open Visual Studio IDE.
Open a console app (.NET Framework).
To find the sum of the entered number, the code is as follows -
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Sum_of_entered_number {
- class Program {
- static void Main(string[] args) {
- int t, n, sum = 0;
- Console.WriteLine("Enter the value of n:");
- n = Convert.ToInt32(Console.ReadLine());
- while (n != 0) {
- t = n % 10;
- sum = sum + t;
- n = n / 10;
- }
- Console.WriteLine("Sum of entered number is: " + sum);
- Console.ReadLine();
- }
- }
- }
Then, after writing the code, the following screen will appear.
And, the output will be -