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 :: c# string to int 
Csharp :: c# datetime blank 
Csharp :: Edit file C# 
Csharp :: unity toint 
Csharp :: uri file path c# 
Csharp :: how to upload an image to an image source c# 
Csharp :: dictionary string list int c# 
Csharp :: how to compare datetime in c# 
Csharp :: c# create excel file 
Csharp :: c# allowedusernamecharacters 
Csharp :: how to load file from resources in c# 
Csharp :: string to chararray c# 
Csharp :: assembly project name c# .net 
Csharp :: c# debug writeline 
Csharp :: unity c# cos inverse 
Csharp :: autofac .net core 6 
Csharp :: linked list reverse 
Csharp :: how to reload app.config file at runtime in c# 
Csharp :: get int value from enum c# 
Csharp :: vb.net get date minus one day 
Csharp :: unity lerp 
Csharp :: create new object c# 
Csharp :: convert c# string to int 
Csharp :: unity detect when an object has been clicked 
Csharp :: How to search values in the registry 
Csharp :: .net 4.5 use tls 1.2 
Csharp :: group-by-in-linq 
Csharp :: c# read xml tag value 
Csharp :: unity scene switch 
Csharp :: update browserslist 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =