Write a program in C-Sharp to find the sum of all the digits of a given number.

The problem here is to sum the digits of a give number for example,
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();
}
}
}

0 comments:

Post a Comment

 

About Me

It's Me!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.

Site Info

ProjectCSharp.com is mainly about demonstrating real time examples in different technologies like ASP.NET, WCF, WPF, Silverlight, Azure etc..,

Followers

Help Links

Project CSharp (C#) Copyright © 2011. All Rights Reserved.