Wednesday, March 20, 2013

C#: Calculate the total of 3 digits from the 3-digit integer n (input from the keyboard).


//Example: Write a program with type Console Application, enter the 3-digit integer n. Please calculate the total 3 digits.
namespace BaiThucHanh1
{
    class Program
    {
        static void Main (){
            int n;
            Console.Write ("Enter n:");
            n = int.Parse (Console.ReadLine ());
            int s = 0;
            int balance;
            while (n! = 0) {
                balance = n % 10;
                s+=balance;
                n = n / 10;
            }
            Console.WriteLine ("The total of 3 digits is: {0}", s.ToString ());
            Console.ReadLine ();
        }
    }
}

No comments:

Post a Comment