Advertisement
coasterka

OperationsWithFourDigitNum

Mar 11th, 2014
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1. using System;
  2.  
  3. namespace OperationsWithFourDigitNum
  4. {
  5.     class OperationsWithFourDigitNum
  6.     {
  7.         static void Main()
  8.         {
  9.             Console.WriteLine("Enter a random four digit number: ");
  10.             int number = int.Parse(Console.ReadLine());
  11.             //detach the four digits; convert them into whole numbers
  12.             int a = number / 1000;
  13.             int b = (number - a * 1000) / 100;
  14.             int c = (number - (a * 1000 + b * 100)) / 10;
  15.             int d = (number - (a * 1000 + b * 100 + c * 10));
  16.             //calculations
  17.             int sum = a + b + c + d;
  18.             //console outpute
  19.             Console.WriteLine("The sum of the four digits is: " + sum);
  20.             Console.WriteLine("Reverse order: {0}{1}{2}{3}", d, c, b, a);
  21.             Console.WriteLine("Last digit in first place: {0}{1}{2}{3}", d, a, b, c);
  22.             Console.WriteLine("Switched 2nd and 3rd positions: {0}{1}{2}{3}", a, c, b, d);
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement