Advertisement
Stann

FormattingNumbers

Mar 19th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using System;
  2.  
  3. class FormattingNumbers
  4. {
  5.     static void Main()
  6.     {
  7.         //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.
  8.  
  9.         Console.Write("Enter integer a: ");
  10.         int intA = int.Parse(Console.ReadLine());
  11.         while(intA < 0 || intA > 500) //Check number is valid
  12.         {
  13.             Console.WriteLine("Invalid input! Try again!");
  14.             intA = int.Parse(Console.ReadLine());
  15.         }
  16.         Console.Write("Enter floating point number b: ");
  17.         decimal decimalB = decimal.Parse(Console.ReadLine());
  18.         Console.Write("Enter floating point number c: ");
  19.         decimal decimalC = decimal.Parse(Console.ReadLine());
  20.         Console.Write("{0,-10:x}",intA);
  21.         Console.Write(Convert.ToString(intA, 2).PadLeft(10,'0'));
  22.         Console.Write("{0,10:F2}",decimalB);
  23.         Console.Write("{0,-10:F3}",decimalC);
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement