CIS 160 printf exercise

Objective

  • Use printf to format output

Exercise

The format strings for each of the printf method calls below are incomplete. Fill in the format strings to get the exact same output as the sample output displayed at the bottom of this page.

public class PrintfExercise { public static void main(String[] args) { int a = 17, b = 127; double x = 123.45678901234; System.out.printf("%n"); System.out.printf("a: %%n", a); System.out.printf("b: %%n", b); System.out.printf("x: %%n", x); System.out.printf("x: %%n", x); System.out.printf("x: %%n", x); System.out.printf("% is % of %%n", 5, (5 * 100 / 10), 10); } }

Sample output

This is the printf exercise a: 17 b: 000000127 x: 123.46 x: 123.4568 x: 1.23e+02 5 is 50% of 10

Solution

The solution is available: PrintfExercise.java