Advertisement
AnitaN

04.ConsoleInputOutputHomework\05.FormattingNumbers

Mar 23rd, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. //Problem 5.Formatting Numbers
  2. //Write a program that reads 3 numbers: an integer a (0 ≤ a ≤ 500), a floating-point b and a floating-point c and prints them in 4 virtual columns on the console. Each column should have a width of 10 characters. The number a should be printed in hexadecimal, left aligned; then the number a should be printed in binary form, padded with zeroes, then the number b should be printed with 2 digits after the decimal point, right aligned; the number c should be printed with 3 digits after the decimal point, left aligned.
  3.  
  4. using System;
  5.  
  6. class FormattingNumbers
  7. {
  8.     static void Main()
  9.     {
  10.         Console.Write("Please, enter  0 <= a <= 500: ");
  11.         int a = int.Parse(Console.ReadLine());
  12.         //Check number is valid (0 ≤ a ≤ 500)
  13.         if (a >= 0 && a <= 500)
  14.         {
  15.             Console.Write("Please, enter b: ");
  16.             decimal b = decimal.Parse(Console.ReadLine());
  17.             Console.Write("Please, enter c: ");
  18.             decimal c = decimal.Parse(Console.ReadLine());
  19.  
  20.             Console.Write("|{0,-10:X}|", a);
  21.             Console.Write(Convert.ToString(a, 2).PadLeft(10, '0'));
  22.             Console.Write("|{0,10:F2}", b);
  23.             Console.Write("|{0,-10:F3}|", c);
  24.             Console.WriteLine();
  25.         }
  26.         else
  27.         {
  28.             Console.WriteLine("Invalid input! Please enter integer number 0 <= a >= 500. Try again!");
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement