Advertisement
simeon_petrov

Homework 5 Problem 6

Sep 12th, 2015
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace HomeWork5_4digits
  8. {
  9. class digits4
  10. {
  11. static void Main(string[] args)
  12. {
  13. /* Write a program that takes as input a four-digit numberin format abcd(e.g. 2011) and performs the following:
  14. Calculates the sum of the digits (in our example 2 + 0 + 1 + 1 = 4).
  15. Prints on the console the number in reversed order: dcba(in our example 1102).
  16. Puts the last digit in the first position:
  17. dabc(in our example 1201).
  18. Exchanges the second and the third digits: acbd(in our example 2101).
  19. The number has always exactly 4 digits and cannot start with 0.Examples:
  20.  
  21. n sum of digits reversed last digit in front second and third digits exchanged
  22. 2011 4 1102 1201 2101
  23. 3333 12 3333 3333 3333
  24. 9876 30 6789 6987 9786*/
  25.  
  26. string nStr = Console.ReadLine();
  27. int sum = 0;
  28. int a = 0; int b = 0; int c = 0; int d = 0;
  29. a = int.Parse(nStr[0].ToString()); b = int.Parse(nStr[1].ToString()); c = int.Parse(nStr[2].ToString()); d = int.Parse(nStr[3].ToString());
  30. sum = a + b + c + d;
  31. //Namirane na sumata
  32. char[] numbers = nStr.ToCharArray();
  33. Array.Reverse(numbers);
  34. string nStrReverse = new string(numbers);
  35.  
  36. string numFirstLast = nStr[3].ToString() + nStr[0] + nStr[1] + nStr[2];
  37.  
  38. string numSecondThird = nStr[0].ToString() + nStr[2] + nStr[1] + nStr[3];
  39.  
  40. Console.WriteLine("| n | sum |revers |LastFir|SecTr |");
  41. Console.WriteLine("|{0,7}|{1,7}|{2,7}|{3,7}|{4,7}|", nStr, sum, nStrReverse, numFirstLast, numSecondThird);
  42.  
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement