Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Problem 5.Formatting Numbers
- //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.
- using System;
- class FormattingNumbers
- {
- static void Main()
- {
- Console.Write("Please, enter 0 <= a <= 500: ");
- int a = int.Parse(Console.ReadLine());
- //Check number is valid (0 ≤ a ≤ 500)
- if (a >= 0 && a <= 500)
- {
- Console.Write("Please, enter b: ");
- decimal b = decimal.Parse(Console.ReadLine());
- Console.Write("Please, enter c: ");
- decimal c = decimal.Parse(Console.ReadLine());
- Console.Write("|{0,-10:X}|", a);
- Console.Write(Convert.ToString(a, 2).PadLeft(10, '0'));
- Console.Write("|{0,10:F2}", b);
- Console.Write("|{0,-10:F3}|", c);
- Console.WriteLine();
- }
- else
- {
- Console.WriteLine("Invalid input! Please enter integer number 0 <= a >= 500. Try again!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement