Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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.
- using System;
- class FourDigit
- {
- static void Main()
- {
- Console.Write("Enter four-digit number = ");
- string numberA = Console.ReadLine();
- int fourDigits = int.Parse(numberA);
- int d = fourDigits % 10;
- int c = ((fourDigits / 10) % 10);
- int b = ((fourDigits / 100) % 10);
- int a = ((fourDigits / 1000) % 10);
- Console.WriteLine("Sum of digits: {0}", a + b + c + d);
- Console.WriteLine("Reversed digits: {0}{1}{2}{3}", d, c, b, a);
- Console.WriteLine("Last digit in front: {0}{1}{2}{3}", d, a, b, c);
- Console.WriteLine("Second and third digits exchanged: {0}{1}{2}{3}", a, c, b, d);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment