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# sum object values 
Csharp :: unity initialize array 
Csharp :: c# inheritance 
Csharp :: convert c# string to int 
Csharp :: mysqldump - date 
Csharp :: longest substring without repeating characters c# 
Csharp :: c# object is in object list 
Csharp :: visitor pattern 
Csharp :: unity get max occurrence in list 
Csharp :: new list/array with values c# 
Csharp :: c# add strings 
Csharp :: c# does value exist in list 
Csharp :: onmousedown not working unity 
Csharp :: get key in dictionary c# 
Csharp :: c# linq select specific columns 
Csharp :: c# $ string 
Csharp :: Remove access to admin from deleting the file in C# 
Csharp :: predicate EF Core search query 
Csharp :: c# centos Regex Username 
Csharp :: regular expression alphanumeric dash space c# 
Csharp :: display array elemetns to text box c# 
Csharp :: link form to a button in dashbord visual c# 
Csharp :: serialize xml as array C# 
Csharp :: administrative priviledge in c# 
Csharp :: sto playing audiosource 
Csharp :: drawing default serializedproperty unity 
Csharp :: unity for loop array 
Csharp :: encode pdf file to base64 c# 
Csharp :: Palindromic substrings 
Csharp :: c# check that value is not null or 0 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =