4. Sum of Digits Program

4.Write a c# program to find sum of digits in a given number.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sumdigits
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, sum;
            Console.WriteLine("Please enter the number");
            n=int.Parse(Console.ReadLine());
            sum = 0;
            while (n != 0)
            {
                sum += n % 10;
                n /= 10;
            }
            Console.WriteLine("the sum of digits is " + sum);

        }
    }
}

4 comments:

  1. all is well with this site but it would be more better if there is an explanation for the programs

    thanks,
    nikkan

    ReplyDelete
  2. a great collection...thanks a lot

    ReplyDelete
  3. it would be really helpful if there is an output for every program...

    ReplyDelete
  4. What is system.Linq, explain any one

    ReplyDelete