Wednesday, 28 October 2015

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 SumOfDigits
    {
        static void Main(string[] args)
        {
            int num, sum = 0, res;
            Console.WriteLine("Enter a Number : ");
            num = int.Parse(Console.ReadLine());
            while (num != 0)
            {
                res = num % 10;
                num = num / 10;
                sum = sum + res;
            }
            Console.WriteLine("Sum of Digits of the Number : "+sum);
            Console.ReadLine();

        }
    }
}
---------------------------
OUTPUT
Enter a Number : 234
Sum of Digits of the Number : 9
--------------

1 comment:

  1. this blog good for us. thanks and keep it.

    ReplyDelete