Advertisement
AnitaN

03.OperatorsExpressionsStatements/6.FourDigitNumber

Mar 20th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. //Problem 6.    Four-Digit Number
  2. //Write a program that takes as input a four-digit number in format abcd (e.g. 2011) and performs the following:
  3. //Calculates the sum of the digits (in our example 2+0+1+1 = 4).
  4. //Prints on the console the number in reversed order: dcba (in our example 1102).
  5. //Puts the last digit in the first position: dabc (in our example 1201).
  6. //Exchanges the second and the third digits: acbd (in our example 2101).
  7. //The number has always exactly 4 digits and cannot start with 0.
  8.  
  9. using System;
  10.  
  11. class FourDigitNumber
  12. {
  13.     static void Main()
  14.     {
  15.         Console.Write("Please enter a four-digit number in format abcd:");
  16.         string myString = Console.ReadLine();
  17.         int stringLenght = myString.Length;
  18.         //Check the lenght of string
  19.         if (stringLenght == 4)
  20.         {
  21.             char a = myString[0];
  22.             char b = myString[1];
  23.             char c = myString[2];
  24.             char d = myString[3];
  25.  
  26.             Console.WriteLine("a={0}", a);
  27.             Console.WriteLine("b={0}", b);
  28.             Console.WriteLine("c={0}", c);
  29.             Console.WriteLine("d={0}", d);
  30.  
  31.             int firstDigit = Convert.ToInt32(new string(a, 1));
  32.             Console.WriteLine("First digit is {0}.", firstDigit);
  33.             //Check the first digit of the number
  34.             if (firstDigit != 0)
  35.             {
  36.                 //Sum of digits
  37.                 int sumDigits = (int)Char.GetNumericValue(a) + (int)Char.GetNumericValue(b) +
  38.                     (int)Char.GetNumericValue(c) + (int)Char.GetNumericValue(d);
  39.                 Console.WriteLine("The Sum of the digits:{0}", sumDigits);
  40.  
  41.                 //Reverse number
  42.                 int newLenghtString = stringLenght - 1;
  43.                 Console.Write("The reverse number in format dcba:");
  44.                 while (newLenghtString >= 0)
  45.                 {
  46.                     string reverseString = "" + myString[newLenghtString];
  47.                     newLenghtString--;
  48.                     Console.Write("{0}", reverseString);
  49.                 }
  50.                 Console.WriteLine();
  51.                 //String in order dabc
  52.                 string lastToFirst = d.ToString() + a.ToString() + b.ToString() + c.ToString();
  53.                 Console.WriteLine("The number in format dabc:{0}", lastToFirst);
  54.                 //String in order acbd
  55.                 string secondToThird = a.ToString() + c.ToString() + b.ToString() + d.ToString();
  56.                 Console.WriteLine("The number in format acbd:{0}", secondToThird);
  57.             }
  58.             else
  59.             {
  60.                 Console.WriteLine("The number cannot start with 0. Please, enter again 4 digits number.");
  61.             }
  62.         }
  63.         else
  64.         {
  65.             Console.WriteLine("The number must be exactly 4 digits. Please, enter again 4 digits number.");
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement