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
What is the relation between Classes and Objects? The class & object look very much same but are not same. The Class is a definition, while object is a instance of the class cr

What is the difference between VB6 and VB.Net? VB6 1.       It is Object-based Language 2.       It Does not support Threading 3.       Not powerful Exception handli

What is a runtime engine? A runtime engine of WF gives the basic functionality to execute and manage the workflow lifetime. It runs within the host process and is responsible f

Develop Nesting software with unit costing for gasket manufacturing Project Description: Our current gasket costing software is old and antiquated and has limited use interna

Custom Mql4 Code/Fxdreema block Project Description: I need the subsequent code written in mql4 and integrated as custom block(s) in fxdreema: for each trade: check pro

The steps that occur when we request a ASPX page are as follows:- 1) The browser sends the request to the webserver. Now  assume that the webserver at the other end is IIS. 2

What are advantages of using UML? As the name suggests the UNIFIED MODELING LANGUAGE, The Modeling has been around for years, not only in the software field but also in other t

How to clear a datagrid on a button click? You require to Clear the DataSource of the dadaGrid. So try this: dataSet1.Clear(); dataGrid1.DataSource = dataSet1.TableNam

What are LINQ query expressions? A LINQ query, also known as a query expression, having of a combination of query clauses that identify the data sources for the query. It conta

Project Description: Step 1: Buy 1 share at existing market price (ex: buy 1 at 100.00) If 4 points profit then close position if not go to step 2 Step 2: if 2 point