Monday, 2 March 2015

// C# Program to Get a Number and Display the Sum of the Digits
    using System;
    class Program
        {
           public static void Main()
            {
                int num;
                Console.WriteLine("Enter a Number : ");
                num = Convert.ToInt32(Console.ReadLine());
                sum s = new sum();
                s.get(num);
            }
        }
        class sum
        {
            public void get(int num)
            {
                int sum = 0, r;
                while (num != 0)
                {
                    r = num % 10;
                    num = num / 10;
                    sum = sum + r;
                }
                Console.WriteLine("Sum of Digits of the Number : " + sum);
                Console.ReadLine();
            }
        }

No comments:

Post a Comment