Advertisement
adriyanbulgary

OperatorsExpressionsAndStatements - Task 6

Jun 13th, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  15.         short number = Convert.ToInt16(Console.ReadLine());
  16.         short digitA = Convert.ToInt16( number % 10);
  17.         short digitB = Convert.ToInt16((number / 10) % 10);
  18.         short digitC = Convert.ToInt16((number / 100) % 10);
  19.         short digitD = Convert.ToInt16( number / 1000);
  20.         short exchangeSecondAndThird = Convert.ToInt16(digitD * 1000 + digitB * 100 + digitC * 10 + digitA);
  21.         short digitsSum = Convert.ToInt16(digitA + digitB + digitC + digitD);
  22.         short reverseNumber;
  23.         short lastDigitInFront;
  24.         Console.Clear();
  25.         Console.WriteLine("\n\n-------------------------------------------------------------------------------");
  26.         Console.WriteLine("| number | sum of digits | reversed | last digit in front | exch second-third |");
  27.         if (digitA == 0)
  28.         {
  29.             if (digitsSum > 10)
  30.             {
  31.                 Console.WriteLine("|  {0}  |       {1}      | last is 0|      last is 0      |       {2}        |", number, digitsSum, exchangeSecondAndThird);
  32.  
  33.             }
  34.             else
  35.             {
  36.                 Console.WriteLine("|  {0}  |       {1}       | last is 0|      last is 0      |       {2}        |", number, digitsSum, exchangeSecondAndThird);
  37.             }
  38.         }
  39.         else
  40.         {
  41.             reverseNumber = Convert.ToInt16((digitD + digitC * 10 + digitB * 100 + digitA * 1000));
  42.             lastDigitInFront = Convert.ToInt16((digitA * 1000 + digitD * 100 + digitC * 10 + digitB));
  43.             if (digitsSum < 10)
  44.             {
  45.                 Console.WriteLine("|  {0}  |       {1}       |   {2}   |         {3}        |        {4}       |", number, digitsSum, reverseNumber, lastDigitInFront, exchangeSecondAndThird);
  46.             }
  47.             else
  48.             {
  49.                 Console.WriteLine("|  {0}  |       {1}      |   {2}   |         {3}        |        {4}       |", number, digitsSum, reverseNumber, lastDigitInFront, exchangeSecondAndThird);
  50.             }
  51.             Console.WriteLine("-------------------------------------------------------------------------------");
  52.         }
  53.         Console.Read();
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement