Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /*
- *Write a program that takes as input a four-digit number in 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
- */
- class FourDigitsNumber
- {
- static void Main()
- {
- Console.Write("Please enter the number:\t");
- short number = short.Parse(Console.ReadLine());
- int digitA = number % 10;
- int digitB = (number / 10) % 10;
- int digitC = (number / 100) % 10;
- int digitD = number / 1000;
- int exchangeSecondAndThird = digitD * 1000 + digitB * 100 + digitC * 10 + digitA;
- int digitsSum = digitA + digitB + digitC + digitD;
- int reverseNumber;
- int lastDigitInFront;
- if (digitA == 0)
- {
- Console.WriteLine("The number is : {0}", number);
- Console.WriteLine("The sum of the digits is : {0}", digitsSum);
- Console.WriteLine("Reversed number : The last digit is 0 !");
- Console.WriteLine("Put last digit in front: The last digit is 0 !");
- Console.WriteLine("After exchange of second and third digit: {0}\n", exchangeSecondAndThird);
- }
- else
- {
- reverseNumber = digitD + digitC * 10 + digitB * 100 + digitA * 1000;
- lastDigitInFront = digitA * 1000 + digitD * 100 + digitC * 10 + digitB;
- Console.WriteLine("The number is : {0}", number);
- Console.WriteLine("The sum of the digits is : {0}", digitsSum);
- Console.WriteLine("Reversed number : {0}",reverseNumber);
- Console.WriteLine("Put last digit in front: {0}",lastDigitInFront);
- Console.WriteLine("After exchange of second and third digit: {0}\n", exchangeSecondAndThird);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement