Advertisement
VyaraG

TakeDigitsFromNumber

Nov 28th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2.  
  3. //Write a program that takes as input a four-digit number in format abcd (e.g. 2011) and performs the following:
  4. //•   Calculates the sum of the digits (in our example 2+0+1+1 = 4).
  5. //•   Prints on the console the number in reversed order: dcba (in our example 1102).
  6. //•   Puts the last digit in the first position: dabc (in our example 1201).
  7. //•   Exchanges the second and the third digits: acbd (in our example 2101).
  8. //The number has always exactly 4 digits and cannot start with 0
  9.  
  10.  
  11. class FourDigitNumber
  12. {
  13.     static void Main()
  14.     {
  15.         Console.Write("Enter a four-digit number: ");
  16.         int n = Math.Abs(int.Parse(Console.ReadLine()));
  17.         int lastDigit = n % 10;
  18.         int thirdDigit = (n / 10) % 10;
  19.         int secondDigit = (n / 100) % 10;
  20.         int firstDigit = (n / 1000) % 10;
  21.         int sumOfDigits = firstDigit + secondDigit + thirdDigit +lastDigit;
  22.         Console.WriteLine("The sum of the digits is: {0}", sumOfDigits);
  23.         Console.WriteLine("The number in reversed order is: {0}{1}{2}{3}", lastDigit, thirdDigit, secondDigit, firstDigit);
  24.         Console.WriteLine("When the last digit comes first, the number is: {0}{1}{2}{3}", lastDigit, firstDigit,secondDigit, thirdDigit);
  25.         Console.WriteLine("When the second an the third digits are exchanged, the number is: {0}{1}{2}{3}", firstDigit, thirdDigit, secondDigit, lastDigit);
  26.        
  27.        
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement