Friday, 16 October 2015

Calculating the sum of a Digits in C#

using System;
class Program
{
    static void Main()
    {
        int sum = 0;
        Console.WriteLine("Enter a Number");
        int Number = int.Parse(Console.ReadLine());
        int temp = Number;
        while (temp > 0)
        {
            sum = sum + (temp % 10);
            temp = temp / 10;
        }
        Console.WriteLine("Sum of {0} Digit is : {1}",Number,sum);

    }
}

No comments:

Post a Comment