Question #64444

Write an algorithm that asks the user to enter a positive integer (validate if the integer is positive example 624). The program calculates and displays on screen the sum of digits of the number. Example: the sum of the digits of the number 624 is 12 (result of the addition of 6 + 2 + 4).
Implement the algorithm in C# program.
1

Expert's answer

2017-01-11T09:35:13-0500
using System;
namespace Question64444
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = -1;
            while (a <= 0)
            {
                Console.WriteLine("Enter positive number");
                a = int.Parse(Console.ReadLine());
            }
            int sum = 0;
            while (a > 0)
            {
                sum += a % 10;
                a /= 10;
            }
            Console.WriteLine("Result: " + sum);
        }
    }
}


Fig. 1 First test



Fig. 2 Second test

Answer provided by www.AssignmentExpert.com

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!
LATEST TUTORIALS
APPROVED BY CLIENTS