Wednesday, June 19, 2013

A C# program to input a number and print sum of its digits.

Print the number one by one...



class Program
{
static void Main(string[] args)
{
int sum = 0,a,b;
a=int.Parse(Console.ReadLine());
for (int i = a; i > 0;i= i / 10)
{
b = i % 10;
sum = sum + b;
}
Console.WriteLine(sum);
Console.ReadLine();
}
}


For Example You type 123 it returns 6 as answer.. 1 + 2 + 3 = 6







No comments:

Post a Comment