Advertisement
adriyanbulgary

OperatorsExpressionsAndStatements - Task 6

Jun 13th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1. using System;
  2. /*
  3. *Write a program that takes as input a four-digit number in format abcd (e.g. 2011) and performs the following:
  4. * Calculates the sum of the digits (in our example 2+0+1+1 = 4).
  5. * Prints on the console the number in reversed order: dcba (in our example 1102).
  6. * Puts the last digit in the first position: dabc (in our example 1201).
  7. * Exchanges the second and the third digits: acbd (in our example 2101).
  8. * The number has always exactly 4 digits and cannot start with 0
  9. */
  10. class FourDigitsNumber
  11. {
  12.     static void Main()
  13.     {
  14.         Console.Write("Please enter the number:\t");
  15.         short number = short.Parse(Console.ReadLine());
  16.         int digitA = number % 10;
  17.         int digitB = (number / 10) % 10;
  18.         int digitC = (number / 100) % 10;
  19.         int digitD = number / 1000;
  20.         int exchangeSecondAndThird = digitD * 1000 + digitB * 100 + digitC * 10 + digitA;
  21.         int digitsSum = digitA + digitB + digitC + digitD;
  22.         int reverseNumber;
  23.         int lastDigitInFront;
  24.         if (digitA == 0)
  25.         {
  26.             Console.WriteLine("The number is : {0}", number);
  27.             Console.WriteLine("The sum of the digits is : {0}", digitsSum);
  28.             Console.WriteLine("Reversed number : The last digit is 0 !");
  29.             Console.WriteLine("Put last digit in front: The last digit is 0 !");
  30.             Console.WriteLine("After exchange of second and third digit: {0}\n", exchangeSecondAndThird);
  31.         }
  32.         else
  33.         {
  34.             reverseNumber = digitD + digitC * 10 + digitB * 100 + digitA * 1000;
  35.             lastDigitInFront = digitA * 1000 + digitD * 100 + digitC * 10 + digitB;
  36.             Console.WriteLine("The number is : {0}", number);
  37.             Console.WriteLine("The sum of the digits is : {0}", digitsSum);
  38.             Console.WriteLine("Reversed number : {0}",reverseNumber);
  39.             Console.WriteLine("Put last digit in front: {0}",lastDigitInFront);
  40.             Console.WriteLine("After exchange of second and third digit: {0}\n", exchangeSecondAndThird);
  41.  
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement