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 FourDigitNumber
- {
- static void Main()
- {
- Console.Write("Enter a four-digit number: ");
- int n = Math.Abs(int.Parse(Console.ReadLine()));
- int lastDigit = n % 10;
- int thirdDigit = (n / 10) % 10;
- int secondDigit = (n / 100) % 10;
- int firstDigit = (n / 1000) % 10;
- int sumOfDigits = firstDigit + secondDigit + thirdDigit +lastDigit;
- Console.WriteLine("The sum of the digits is: {0}", sumOfDigits);
- Console.WriteLine("The number in reversed order is: {0}{1}{2}{3}", lastDigit, thirdDigit, secondDigit, firstDigit);
- Console.WriteLine("When the last digit comes first, the number is: {0}{1}{2}{3}", lastDigit, firstDigit,secondDigit, thirdDigit);
- Console.WriteLine("When the second an the third digits are exchanged, the number is: {0}{1}{2}{3}", firstDigit, thirdDigit, secondDigit, lastDigit);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement