C# Sum - c# - c# tutorial - c# net




Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

How to write Sum Program in C# ?

  • The C# Program Gets a Number and Display the Sum of the Digits. The digit sum of a given integer is the sum of all its digits.
  • We can write the sum of digits program in C# language by the help of loop and mathematical operation only.
 C# Sum

C# Sum

Sum of digits algorithm

  • To get sum of each digit by C# program, use the following algorithm:
    • Step 1: Get number by user
    • Step 2: Get the modulus/remainder of the number
    • Step 3: sum the remainder of the number
    • Step 4: Divide the number by 10
    • Step 5: Repeat the step 2 while number is greater than 0.
  • Let's see the sum of digits program in C#

Example 1:

using System;  
  	public class SumExample  
   {  
     public static void Main(string[] args)  
      {  
       int  n,sum=0,m;         
       Console.Write("Enter a number: ");      
       n= int.Parse(Console.ReadLine());     
       while(n>0)      
       {      
        m=n%10;      
        sum=sum+m;      
        n=n/10;      
       }      
       Console.Write("Sum is= "+sum);       
     }  
  } 
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

Enter a number: 23  
Sum is= 5
Enter a number: 624       
Sum is= 12

Example 2:

/*
 * C# Program to Get a Number and Display the Sum of the Digits 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, sum = 0, r;
            Console.WriteLine("Enter a Number : ");
            num = int.Parse(Console.ReadLine());
            while (num != 0)
            {
                r = num % 10;
                num = num / 10;
                sum = sum + r;
            }
            Console.WriteLine("Sum of Digits of the Number : "+sum);
            Console.ReadLine();
 
        }
    }
}
click below button to copy the code. By - c# tutorial - team
Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

C# examples - Output :

Enter a Number: 123
Sum of Digits of the Number: 6


Related Searches to C# Sum

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×