Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

sum of digits in c#

int num = 123;
int sum = 0;
while(num > 0)
{
  sum += number % 10;
  num /= 10;
}
Console.WriteLine(sum); // output: 6
Comment

C# calculate sum of digits of a number

using System;
					
public class Program
{
	public static int SumDigits(int inputInt)
	{
		string inputString = inputInt.ToString();
		int sumDigits = 0;
		
		for(int i = 0; i < inputString.Length; i++)
		{
			int currentDigit = int.Parse(inputString[i].ToString());
			sumDigits += currentDigit;
		}
		return sumDigits;
	}
	public static void Main()
	{
		//test value -> output 30
		Console.WriteLine(SumDigits(464646));
	}
}
Comment

c# Program for Sum of the digits of a given number

// C# program to compute
// sum of digits in number.
using System;
 
class GFG {
    /* Function to get sum of digits */
    static int getSum(int n)
    {
        int sum = 0;
 
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
 
        return sum;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 687;
        Console.Write(getSum(n));
    }
}
 
Comment

sum the digits in c#

sum = 0;
while (n != 0) {
    sum += n % 10;
    n /= 10;
}
Comment

sum of digit of number c#

int result = 17463.ToString().Sum(c => Convert.ToInt32(c));
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity master volume changer 
Csharp :: c# bool to int 
Csharp :: unity pause coroutine 
Csharp :: c# write iformfile 
Csharp :: remove header visual studio android 
Csharp :: longest substring without repeating characters leetcode 
Csharp :: wpf keyboard press event 
Csharp :: frustum 
Csharp :: c# get random index from list 
Csharp :: finding values in the registry 
Csharp :: C# random.Next error 
Csharp :: div element position in screen 
Csharp :: c# split include separators 
Csharp :: httpclient 
Csharp :: c# windows forms cancel event 
Csharp :: c# read xml tag value 
Csharp :: how to set a tag in asp net razor view stackoverflow 
Csharp :: ASP.net ApplicationUser referance not found 
Csharp :: how to do that a objetct moves in c# 
Csharp :: Screen.lockcursor unity 
Csharp :: C# webclient submit form 
Csharp :: unity rigidbody freeze all rotation 
Csharp :: c# remove xml invalid characters 
Csharp :: demand a Security action c# 
Csharp :: c# lambda group by multiple columns 
Csharp :: load a form from button c# 
Csharp :: vb.net delete folder if exists 
Csharp :: how to count letters in c# 
Csharp :: c# timer single tick 
Csharp :: c# async and await example 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =