Latest Post

Tuesday, 3 January 2017

Find the sum of digits of the entered number[C#]

Write a program in C# to find the sum of digits of the entered number.

Following is the simple C# code :
class Lab
    {
        static void Main(string[] args)
        {
            int number, sum = 0, r;
            Console.WriteLine("Enter a Number : ");
            number = int.Parse(Console.ReadLine());
            while (number != 0)
            {
                r = number % 10;
                number = number / 10;
                sum = sum + r;
            }
            Console.WriteLine("Sum of Digits of the Number : " + sum);
            Console.ReadLine();

        }
    }

Output:-
Enter a Number :
15
Sum of Digits of the Number : 6

Enter a Number :
5423
Sum of Digits of the Number : 14


Government Jobs