P12 To find the sum of digits in an integer



    class Program
    {
        static void Main(string[] args)
        {
            int n, r, ans = 0; ;
            Console.Write("Enter the number : ");
            n = int.Parse(Console.ReadLine());
            while (n > 0)
            {
                r = n % 10;
                ans = ans + r;
                n = n / 10;
            }
            Console.WriteLine("The sum of the digits in the integer is " + ans);
            Console.ReadLine();
        }
    }

Output : 



No comments:

Post a Comment