Calculating the sum of digits - c# program, DOT NET Programming

Calculating the Sum of Digits - C# Program

Hi,
Can any of you, tell me how can i calculate the sum of digits of a given number?

Posted Date: 2/11/2013 11:57:32 PM | Location : United States





You might check out this, this is useful for you

Program:

using System;  

class SumOfNumbers

{

public static void Main()

{

int num,rem,i,counter=0,temp,sum=0;

// num : Contains the actual number inputted via the console

// rem : remainder of the number ''num'' when divided by 10

// i : loop variable

// counter : determines the no. of digits in the inputted number ''num''

// temp : temporary variable used to save the value of ''num'' (Explained further)

Console.Write("Enter an integer number (Not more than 9 digits) : ");

num = int.Parse(Console.ReadLine());    

temp = num;

// Here we are saving ''num'' in ''temp'' coz its value after determining the no. of digits will loose.

// So after its work is done, ''num'' will contain value = 0

// The value of ''num'' is resetted to its original value later from ''temp'' variable

//  determine the no. of digits in the inputted number

while(num > 0)

{

rem = num % 10;

num = num / 10;

if (num <= 0) 

{

break;

}

else

{

counter = counter + 1;

}

}    

Console.WriteLine("Number of digits are = " + (counter+1));  

rem = 0;

// resetting the value of remainder ''rem''  

num = temp;

// resetting the lost value of ''num'' from ''temp''

// determine the reversed of inputted digits

// Funda :

// 1) Divide the number by 10 & determine the remainder. (Save the remainder in ''rem'') 

// This will give us the last digit in the actual inputted number. (Same as reversing numbers logic)

// 2) Add the number so obtained into the variable ''sum''

// 3) Divide the same number by 10 & get the quotient this time. 

// Since division is between the integers, we will get the new number, deprived of the last digit.

// Then again goto step 1) & continue until & unless the counter is equal to ''i'' (coz thats the loop varibale)  

for(i = 0; i<=counter; i++)

{

rem = num % 10;

sum = sum + rem;

num = num / 10;   

}

Console.WriteLine("Sum = " +sum);

Console.ReadLine();  

}

}

Posted by Aana | Posted Date: 2/12/2013 12:00:41 AM


Related Discussions:- Calculating the sum of digits - c# program, Assignment Help, Ask Question on Calculating the sum of digits - c# program, Get Answer, Expert's Help, Calculating the sum of digits - c# program Discussions

Write discussion on Calculating the sum of digits - c# program
Your posts are moderated
Related Questions
I want Web Social Apps/ site Project Description: I need a web designer have a specific unique project in mind.  Skills required are C# Programming, Cocoa, Mac OS, iPhone,

What are benefits and Limitation of using Viewstate for state management The benefits of using Viewstate are as follows:- 1)    There is no server resources required because

Mention the name of the directory where it is essential to locate the proxy file to use a Web service. The proxy file must be kept in the /bin directory. This directory is situ

Print Pyramids of Numbers - C# Program Hello can you please provide me some examples regarding to the Print Pyramids of Numbers in C#.

What is Asynchronous One-Way Calls The One-way calls are a different from the asynchronous calls by the execution angle that the .NET Framework does not assure their execution.

how socket workes in .net

What is a monitor object? Monitor objects are used to ensure that a block of code runs without being interrupted by code running on other threads. In other words, code in other

Write a C# program (C# console application) that plays a word game with the user. The program should ask the user to enter the following: • His or her name • His or her age

I need Online Real Commission Management system Project Description: Real Estate -- Store and retrieve Purchase plot customers, Employee to refer the customers get commiss

recipe house Project Description: Web application with sql database Skills required are .NET, C# Programming, HTML, PHP, SQL