Sunday, July 13, 2014

C#: Calculate the sum of digits of N have 8 digits

/*  Enter an integer n has 8 digits. Take a look: 
- The total value of the digits 
- The value of the total unit 
- The average of the total   */
Source code:
class Program{
static void Main(){
            int n;
            Console.Write("Input n has 8 digits: ");
            n = int.Parse(Console.ReadLine());
            //The total value of the digits
            int s = 0;
            int balance;
            int first = n;
            while (n != 0) {
                balance = n % 10;
                s += balance;
                n /= 10;
            }
            //The value of the total unit
            int unit;
            unit = s % 10;
            //The average of the total
            double avg;
            avg = (double) s / 8;
            Console.WriteLine("The total value of the digits {0} is: {1}",first,s);
            Console.WriteLine("The value of the total unit {0} is: {1}",s,unit);
            Console.WriteLine("The average of the total {0} is: {1}",s,avg);
            Console.ReadLine();
        }
}

No comments:

Post a Comment