Search code examples
javamathuser-input

Printing back calculations based on user input


So I am trying to get an output similar to this:

Enter the first number: 3.25

Enter the second number: 10.3

  3.25 *  10.30 =  33.48

  3.25 /  10.30 =   0.32

  3.25 %  10.30 =   3.25

 10.30 *   3.25 =  33.48

 10.30 /   3.25 =   3.17

 10.30 %   3.25 =   0.55

In this example the user inputs the numbers 3.25 and 10.3, then the code takes the numbers and applies several calculations to them.

So here is what I've got so far:

import java.util.Scanner;

public class test {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

double fnumber, snumber, mult, div, modulo, mult2, div2, modulo2;
System.out.println("Enter the first number:");
fnumber = scan.nextDouble();

System.out.println("Enter the second number:");
snumber = scan.nextDouble();

/* various calculations  */
mult = fnumber * snumber;
div = fnumber / snumber;
modulo = fnumber % snumber;
mult2 = snumber * fnumber;
div2 = snumber / fnumber;
modulo2 = snumber % fnumber;

/* print back to user  */
System.out.printf("%f * %f = %.2f\n", mult);

I'm good until this last line. From the example above I am trying to make it say 3.25 * 10.30 = 33.48.

how do i use %f to call on multiple doubles based on user input?

Thanks.


Solution

  • You almost have it. Try this:

    System.out.printf("%f * %f = %f\n", fnumber, snumber, mult);