Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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. */
- import java.util.Locale;
- import java.util.Scanner;
- public class _06_FormattingNumbers {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Locale.setDefault(Locale.ROOT);
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter a Integer number in the range [0 ... 500] for numA: ");
- int numA = scan.nextInt();
- if (numA >= 0 && numA <= 500) {
- System.out.println("Enter 2 Real numbers for numB and numC, separated by a space:");
- double numB = scan.nextDouble();
- double numC = scan.nextDouble();
- String hexadecimalStr = Integer.toHexString(numA).toUpperCase();
- System.out.printf("|%-10s|", hexadecimalStr);
- String binaryStr = Integer.toBinaryString(numA);
- int binaryNum = Integer.parseInt(binaryStr);
- System.out.printf("%010d|", binaryNum);
- System.out.printf("%10.2f|", numB);
- System.out.printf("%-10.3f|\n", numC);
- } else {
- System.out.println("Error! - The numA is Out of Range!!!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement