Q:

Write C# program to print sum of digits enter by user

0

Write C# program to print sum of digits enter by user

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
public class csharpExercise
{
    static void Main(string[] args)
    {
 
        int num, total;
 
        //Reading number
        Console.WriteLine("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());
 
        //Adding sum of digit in total variable
        for (total = 0; num > 0; num = num / 10)
            total = total + (num % 10);
 
        //Printing sum of digit
        Console.WriteLine("Sum of digits: "+ total);
 
        Console.ReadLine();
    }
}

Result:

Enter any number: 

515

Sum of digits: 11

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C# program to find sum of even numbers betwe... >>
<< Write C# program to print all natural numbers in r...