C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

Multiply two floating values - Java Program

import java.util.Scanner;

 

public class MultiplyFloat {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

        // Taking two float values as input

        System.out.print("Enter first floating number: ");

        float num1 = sc.nextFloat();

 

        System.out.print("Enter second floating number: ");

        float num2 = sc.nextFloat();

 

        // Multiplying the two float values

        float result = num1 * num2;

 

        // Displaying the result

        System.out.println("The product of " + num1 + " and " + num2 + " is: " + result);

 

        sc.close();

    }

}

Output

 
OUTPUT 1:
Enter first floating number: 5.6
Enter second floating number: 4.3
The product of 5.6 and 4.3 is: 24.08

OUTPUT 2:
Enter first floating number: 1.5
Enter second floating number: 2.2
The product of 1.5 and 2.2 is: 3.3

Explanation

1. Read two floating numbers

float num1 = sc.nextFloat();

float num2 = sc.nextFloat();

The program uses Scanner to take float input from the user.

2. Perform multiplication

float result = num1 * num2;

Floating-point multiplication is done just like integer multiplication, but the result can contain decimals.

3. Display output

System.out.println("The product of " + num1 + " and " + num2 + " is: " + result);