Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace HomeWork5_4digits
- {
- class digits4
- {
- static void Main(string[] args)
- {
- /* Write a program that takes as input a four-digit numberin format abcd(e.g. 2011) and performs the following:
- Calculates the sum of the digits (in our example 2 + 0 + 1 + 1 = 4).
- Prints on the console the number in reversed order: dcba(in our example 1102).
- Puts the last digit in the first position:
- dabc(in our example 1201).
- Exchanges the second and the third digits: acbd(in our example 2101).
- The number has always exactly 4 digits and cannot start with 0.Examples:
- n sum of digits reversed last digit in front second and third digits exchanged
- 2011 4 1102 1201 2101
- 3333 12 3333 3333 3333
- 9876 30 6789 6987 9786*/
- string nStr = Console.ReadLine();
- int sum = 0;
- int a = 0; int b = 0; int c = 0; int d = 0;
- a = int.Parse(nStr[0].ToString()); b = int.Parse(nStr[1].ToString()); c = int.Parse(nStr[2].ToString()); d = int.Parse(nStr[3].ToString());
- sum = a + b + c + d;
- //Namirane na sumata
- char[] numbers = nStr.ToCharArray();
- Array.Reverse(numbers);
- string nStrReverse = new string(numbers);
- string numFirstLast = nStr[3].ToString() + nStr[0] + nStr[1] + nStr[2];
- string numSecondThird = nStr[0].ToString() + nStr[2] + nStr[1] + nStr[3];
- Console.WriteLine("| n | sum |revers |LastFir|SecTr |");
- Console.WriteLine("|{0,7}|{1,7}|{2,7}|{3,7}|{4,7}|", nStr, sum, nStrReverse, numFirstLast, numSecondThird);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement