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 stop animation from playing at start 
Csharp :: how to check if a path is a directory or file c# 
Csharp :: visual studio console clear 
Csharp :: assign color to value in c# 
Csharp :: c# sum of list 
Csharp :: order by length descending C# 
Csharp :: how to generate random number in unity 
Csharp :: can you have multiple statement in a case c# 
Csharp :: c# named parameters 
Csharp :: flip a character in unity 
Csharp :: unity play sound effect 
Csharp :: swap two numbers c# 
Csharp :: c# run cmd hidden 
Csharp :: No migrations configuration type was found in the assembly 
Csharp :: CS0101 Unity Error Code 
Csharp :: route attribute controller with parameter asp.net core 
Csharp :: c# concatenation 
Csharp :: variable gameobject unity 
Csharp :: unit test c# exception thrown 
Csharp :: datetimeoffset to datetime c# 
Csharp :: c# ienumerable to list 
Csharp :: c# create console for winform 
Csharp :: how to know character is a digit or not in c# 
Csharp :: how to make an ui to follow gameobject 
Csharp :: get all components of type unity 
Csharp :: Change Level in Unity 
Csharp :: textblock line break 
Csharp :: how to chceck for a tag in a trigger enter 2d unity 
Csharp :: how to get row index of selected row in gridview asp.net webforms 
Csharp :: Show private fields in Unity Inspector 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =