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

Assignment Help:

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?


Related Discussions:- Calculating the sum of digits - c# program

I need unity 3d texture painting plug-in, Project Description: I require...

Project Description: I require a custom texture painting plug-in for the latest version of Unity 3D. The project can have features that are common in Photoshop and Mari for pain

Develop mobile app with networking elements, I need to create the subsequen...

I need to create the subsequent iOS App/ iPhone (to be followed by Android & other platforms). Version 1.0--Users can: --Find events based on proximity/zipcode*/ location

Create a xslt and css file, Using the attached XML file (xml_ind.xml) creat...

Using the attached XML file (xml_ind.xml) create a XSLT and CSS file for the following information: select the following information for ONLY the cars that cost more than $50,00

What are the criteria necessary for an effective network, What are the crit...

What are the criteria necessary for an effective and efficient network? a. Performance It can be measured in several ways, including transmit time and response time. b

What is common language runtime, What is "Common Language Runtime" (CLR)?  ...

What is "Common Language Runtime" (CLR)?  CLR is .NET equivalent of Java Virtual Machine (JVM). It is the runtime that changes a MSIL code into the host machine language code,

Jquery and mvc 4.5 training session, Jquery and MVC 4.5 Training Session ...

Jquery and MVC 4.5 Training Session I am looking who have industry experience working on Microsoft Technology ( MVC 4.5 ) and Jquery, who will take live online session and provi

Show the structure of the global.asax file, Question: (a) Write extrac...

Question: (a) Write extract codes to show the structure of the Global.asax file. (b) Outline four classes that allow you to work with File Streams. (c) When working with

Explain the new features in ado.net entity framework 4.0, Explain the new f...

Explain the new features in ADO.NET Entity Framework 4.0. ADO.NET Entity Framework 4.0 is introduced in .NET Framework 4.0 and contain the following new features: Persisten

Microsoft application blocks, Have you ever worked with Microsoft Applicati...

Have you ever worked with Microsoft Application Blocks, if yes then which? The Application Blocks are C# & the VB.NET classes distributed as the Visual Studio projects which ca

Aana

2/12/2013 12:00:41 AM

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();  

}

}

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd