Advertisement
enevlogiev

FourDigits

Mar 10th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. using System;
  2.  
  3. class FourDigitNumber
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         Console.WriteLine("Please enter \"abcd\" - Four-Digit Number");
  8.         int number = int.Parse(Console.ReadLine());
  9.              
  10.         bool hasFourDigits = (number.ToString().Length == 4);        
  11.         while (!hasFourDigits)
  12.         {
  13.             Console.WriteLine("Please write a four-digit number:");
  14.             number = int.Parse(Console.ReadLine());
  15.             hasFourDigits = (number.ToString().Length == 4);
  16.         }
  17.        
  18.         int a = (number / 1000) % 10;
  19.         int b = (number / 100) % 10;
  20.         int c = (number / 10) % 10;
  21.         int d = number % 10;
  22.         Console.Write("The sum of the digits of the number you have just entered is : ");
  23.         Console.WriteLine(a + b + c + d);
  24.         Console.WriteLine("Print on the console the number in reversed order: {0}{1}{2}{3}", d, c, b, a);
  25.         Console.WriteLine("Puts the last digit in the first position: {0}{1}{2}{3}", d, a, b, c);
  26.         Console.WriteLine("Exchanges the second and the third digits: {0}{1}{2}{3}", a, c, b, d);    
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement