Advertisement
Razhagal

FourDigitNumber

Mar 17th, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. class FourDigitNumber
  3. {
  4.     static void Main()
  5.     {
  6.         Console.WriteLine("Enter a four-digit number to see some \"magic\" with it.");
  7.         Console.WriteLine("The number musn't start with 0.");
  8.         Console.WriteLine();
  9.         Console.Write("Your number: ");
  10.         string inputNum = Console.ReadLine();
  11.         int number = int.Parse(inputNum);
  12.  
  13.         if (inputNum.Length > 4)
  14.         {
  15.             Console.WriteLine("The input number is more than 4 digits long! Try again.");
  16.             return;
  17.         }
  18.  
  19.         int a = number / 1000;
  20.         a %= 10;
  21.  
  22.         int b = number / 100;
  23.         b %= 10;
  24.  
  25.         int c = number / 10;
  26.         c %= 10;
  27.  
  28.         int d = number % 10;
  29.  
  30.         if (a == 0)
  31.         {
  32.             Console.WriteLine("The number musn't start with 0! Try again.");
  33.             return;
  34.         }
  35.  
  36.         int sumOfDigits = a + b + c + d;
  37.         Console.WriteLine("The sum of the digits is {0}", sumOfDigits);
  38.         Console.WriteLine("Your number in reversed order is " + d + c + b + a);
  39.         Console.WriteLine("Your number with last digit in front is " + d + a + b + c);
  40.         Console.WriteLine("Your number with second and third digits exchanged is " + a + c + b + d);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement