Answer to Question #256009 in C# for Umar farouk

Question #256009
Sum the digits in an integer) Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is 932, the sum of all its digits is 14. Hint: Use the % operator to extract digits, and use the / operator to remove the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93.
1
Expert's answer
2021-10-27T00:47:43-0400
using System;
using System.Collections.Generic;


namespace App
{






    class Program
    {


        static void Main(string[] args)
        {
            int n = -1;


            while (n < 0 || n > 1000)
            {
                Console.Write("Enter an integer between 0 and 1000: ");
                n = int.Parse(Console.ReadLine());
            }


            int sum = 0;


            while (n != 0)
            {
                sum = sum + n % 10;
                n = n / 10;
            }
            Console.WriteLine("The sum of the digits is : {0}", sum.ToString());


            Console.ReadLine();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS