63 812
Assignments Done
99,5%
Successfully Done
In August 2018

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)
Expert's answer

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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions