Answer to Question #64443 in C# for ceazer

Question #64443
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)
1
Expert's answer
2017-01-10T09:36:14-0500

using System;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
int integer = -1;
string input = "";
while (integer < 0){

Console.WriteLine("Enter a positive integer: ");
input = Console.ReadLine();
integer = Convert.ToInt32(input);

}

int result = 0;
int temp = 0;
for (int i = 0; i < input.Length; ++i)
if (Int32.TryParse(input[i].ToString(), out temp))
result += temp;

Console.WriteLine("Sum of the digits of the number "+input+" is "+result);

}
}
}

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
New on Blog
APPROVED BY CLIENTS