if the number x is input as 123 the result should be calculated as 1 + 2 + 3 = 6. To Implement this logic in C, C++, C-Sharp we should use the Modulo operator. For example..,
using System;
using System.Collections.Generic;
using System.Text;
namespace SumOfDig
{
class Program
{
static void Main(string[] args)
{
long modulo,sum=0;
Console.Write("Enter the Number:");
long number = long.Parse(Console.ReadLine());
while (number > 0)
{
modulo = (number % 10);
sum = sum + modulo;
number = number / 10;
}
Console.WriteLine("The Sum of Digits of number is {0}",sum);
Console.ReadLine();
}
}
}
Hi, I'm Moinuddin. I'm a Software Engineer at WIPRO working on Microsoft .NET Technology. I'm interested in a wide range of technology topics, mainly Microsoft including Windows Azure, WCF, WPF, Silverlight and any other cool technology that catches the eye.
0 comments:
Post a Comment