What is float in Java?

In java, Float is primitive data type having number with a decimal point. It contains number of fractional parts  from 3.4e−038 to 3.4e+038. Note that you should end the value with an “f”.

There are two types of primitive floating point.

1.Float

The float data type requires 4 bytes means 32-bits for their storage. The number of digit after decimals point are know as precision are 7. The range of the precision lies between -3.4E38 to +3.4E38.

For Example:

public class Main {
  public static void main(String[] args) {
    float myNum = 2.89f;
    System.out.println(myNum);  
  }
}

Output:

2.89

2.Double

When we need to work with large decimal numbers, we use Double floating data type. It use 8 bytes means 64-bits for their storage. Its digits precision are 15. The range of the precision lies between -1.7e308 to +1.7e308. Note that you should end the value with a “d”.

For example:

public class Main {
  public static void main(String[] args) {
    double myNum = 21.99d;
    System.out.println(myNum);  
  }
}

Output:

21.99
Floating-point typeSizeMinimum ValueMaximum valueDigits of precision
float32-bit
(4bytes)
-3.4e38+3.4e387
double64-bit
(8bytes)
-1.7e308+1.7e30815
size and range of floating point types.

find multiples of a float value in Java

Here is the simple way to multiples two float number. Let’s see an example.

import java.util.Scanner;
public class Main  {
  public static void main(String[] args) {
  float a= 14.5f;
        float b = 2.40f;

        float number = a * b;

        System.out.println("The number is: " + number);
  }
}  

Output:

The number is: 34.800003

Explanation:

  • In the above example. We have two variable a and b with the value 14.5f and 2.40. F is used to represent that the numbers are type- float otherwise they will assigned as type-double.
  • * is an arithmetic operator, used to multiples the variable a and b. And the result will store in a new variable is called number.
  • Finally, the result of number in printed on the screen by using println() function.

Using a for loop. Let’s see an example.

We have to accept any number they want to add from user as float value and then by using for loop we can find each multiples of that number. We can accept number of multiples from user and then by using no of multiples we can displays multiples of particular float value.

/**
 * Java program to explain how to find multiples of a float value in java.
 */

package cf.java.queshub;

import java.util.Scanner;

public class MultiplesOfAFloatValue {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		// Accept any number from user
		System.out.println("Enter any Float value: ");
		Float num = sc.nextFloat();
		// Accept no of multiples user want
		System.out.println("Enter no of multiples of '"+ num + "' you want: ");
		int noOfMultiples = sc.nextInt();

		System.out.print("Multiples of '" + num + "' are:");
		// Use for loop iterate from 1 to no of multiples given by user
		for(int i = 1; i <= noOfMultiples; i++) {
			// Display multiples
			System.out.print(" " + num * i);
		}

	}

}

Output

Enter any Float value:
12
Enter no of multiples of '12.0' you want:
5
Multiples of '12.0' are: 12.0 24.0 36.0 48.0 60.0

Explanation:

  • In above program, we have asked user to give any value they want. We will use this value as float type to find it’s multiples. As float type Float num = sc.nextFloat();
  • Then we have accepted no of multiples of given number user want using int noOfMultiples = sc.nextInt();
  • We use for loop from 1 to no of multiples that user has given. So that by using these numbers, we will multiply with given number to find multiplesfor(int i=0; i <= noOfMultiples; i++)
  • Finally, we have displayed each multiple inside for loop by using System.out.print(" " + num * i);

Recommended posts

How to define constant in java

What is JavaScript?

Difference between the Java and JavaScript

Implementation of Array class in JavaScript

jQuery Introduction

Reference

codingface.com

Leave a Comment