pupesko

SoftUni - 5.6 Four-Digit Number

Oct 10th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. //Write a program that takes as input a four-digit number in format abcd (e.g. 2011) and performs the following:
  2. Calculates the sum of the digits (in our example 2+0+1+1 = 4).
  3. Prints on the console the number in reversed order: dcba (in our example 1102).
  4. Puts the last digit in the first position: dabc (in our example 1201).
  5. Exchanges the second and the third digits: acbd (in our example 2101).
  6. The number has always exactly 4 digits and cannot start with 0.
  7.  
  8. using System;
  9.  
  10. class FourDigit
  11. {
  12.     static void Main()
  13.     {
  14.         Console.Write("Enter four-digit number = ");
  15.         string numberA = Console.ReadLine();
  16.         int fourDigits = int.Parse(numberA);
  17.  
  18.         int d = fourDigits % 10;
  19.         int c = ((fourDigits / 10) % 10);
  20.         int b = ((fourDigits / 100) % 10);
  21.         int a = ((fourDigits / 1000) % 10);
  22.  
  23.         Console.WriteLine("Sum of digits: {0}", a + b + c + d);
  24.         Console.WriteLine("Reversed digits: {0}{1}{2}{3}", d, c, b, a);
  25.         Console.WriteLine("Last digit in front: {0}{1}{2}{3}", d, a, b, c);
  26.         Console.WriteLine("Second and third digits exchanged: {0}{1}{2}{3}", a, c, b, d);
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment