coasterka

#3OperationsWithFourDigitNum

Mar 13th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. static void Main()
  2.         {
  3.             Console.WriteLine("Enter a random four digit number: ");
  4.             int number = int.Parse(Console.ReadLine());
  5.             //detach the four digits; convert them into whole numbers
  6.             int a = number / 1000;
  7.             int b = (number - a * 1000) / 100;
  8.             int c = (number - (a * 1000 + b * 100)) / 10;
  9.             int d = (number - (a * 1000 + b * 100 + c * 10));
  10.             //calculations
  11.             int sum = a + b + c + d;
  12.             //console output
  13.             Console.WriteLine("Sum of digits: " + sum);
  14.             Console.WriteLine("Reversed: {0}{1}{2}{3}", d, c, b, a);
  15.             Console.WriteLine("Last digit in front: {0}{1}{2}{3}", d, a, b, c);
  16.             Console.WriteLine("Second and third digits exchanged: {0}{1}{2}{3}", a, c, b, d);
  17.         }
Advertisement
Add Comment
Please, Sign In to add comment