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 :: check if ienumerable is empty c# 
Csharp :: c# array last element 
Csharp :: read configuration workerservice 
Csharp :: currentTimeMillis c# 
Csharp :: how to store more data than doublec# 
Csharp :: discord bot time C# 
Csharp :: unity google play games plugin spam 
Csharp :: c# print array 
Csharp :: c# conver date utc to cst 
Csharp :: winforms messagebox with button 
Csharp :: unity temperature to colour 
Csharp :: Request.ServerVariables["HTTP_X_FORWARDED_FOR"] get only one ipaddress 
Csharp :: c# convert utc to est 
Csharp :: remove focus from button unity 
Csharp :: camera follow player 
Csharp :: how to make a object disapear in windows form c# 
Csharp :: check if current time is in the morning c# 
Csharp :: ensuresuccessstatuscode exception 
Csharp :: C# delete last enviroment new line 
Csharp :: unity c# check if multiple keys are pressed 
Csharp :: transfer ownership photon2 
Csharp :: rotation facing mouse unity 
Csharp :: convert string to array c# 
Csharp :: c# convert string to enum 
Csharp :: delete file from FTP c# 
Csharp :: c# convert object to string 
Csharp :: c# dictionary first 
Csharp :: google sheets sum if check contains string 
Csharp :: c# combobox selectedvalue 
Csharp :: c# file dialog to get folder path 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =