How To Find Multiples Of A Float Value In Java?

In this post, we will discuss how to find multiples of a float value in java. A number N is a multiple of another number M if N will be divisible by M.

We have also covered below topics or questions asked by user readers:

  • How to find multiples of a value in Java?
  • What is float in Java?
  • What are the range and the default value of float in Java?

What is Float in Java?

Float is a primitive data type of type double having a range from 1.4013e-45 to 3.4023e38. As float is of type double, So to represent a float value we have to use suffix literal with ‘f’ or ‘F‘.

Get more information about Float value in Oracle Java Documentation.

Data TypeSize[byte(s)]RangeDefault Value
float41.40129846432481707e-45 to 3.40282346638528860e380.0

Examples of float values:

Float Values: 10.0f, 5.6F, 20.0f, 34.5f, etc

You can use both suffixes small ‘f’ or capital ‘F’ with double numbers to represent it as a float value.

Read Also: How To Reverse A String In Java – Learn 5 Easy Methods

How to find multiples of a Float value in Java?

To find multiples of a float, we have to accept any number 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 user want and then by using no of multiples we can displays multiples of particular float value.

Approach-1: How to find multiples of a float value in Java using for loop?

In this approach, we will use for loop to find multiples of a float value. By using for loop, we will iterate from 1 to no of multiples of given float number that the user wants. Let’s see the program by using for loop.

Java program to find multiples of float value using for loop:

/** * Java program to explain how to find multiples of a float value in java. * Question Hub */ package cf.java.number; import java.util.Scanner; public class MultiplesOfAFloatValueUsingForLoop { 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); } } }
Code language: PHP (php)

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
Code language: JavaScript (javascript)

Explanation of above program:

  • In above program, we have asked user to give any value. We will use this value to find it’s multiples. We have accepted this value as float type Float num = sc.nextFloat();
  • Then we have accepted no of multiples of given number user want using int noOfMultiples = sc.nextInt(); (Line No. 16).
  • We have used 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 multiples. for(int i=0; i <= noOfMultiples; i++)
  • Finally, we have displayed each multiple inside for loop by using System.out.print(” ” + num * i);

Read Also: Palindrome In Java(String & Number) – Check 2 Easy Methods

Approach-2: How to find multiples of a float value in Java using while loop?

In this approach, we will use while loop to find multiples of a float value. By using while loop, we will iterate till the value of no of multiples becomes 1. Let’s see the program by using a while loop.

Java Program to find multiples of float value using while loop:

/** * Java program to explain how to find multiples of a float value in java. * Question Hub */ package cf.java.number; import java.util.Scanner; public class MultiplesOfAFloatValueUsingWhileLoop { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); // Accept any number from user System.out.print("Enter any Float value: "); Float num = sc.nextFloat(); // Accept no of multiples user want System.out.print("Enter no of multiples of '"+ num + "' you want: "); int noOfMultiples = sc.nextInt(); System.out.print("Multiples of '" + num + "' are:"); int temp = 1; // Use while loop iterate till the value of no of multiples becomes 1. while(temp <= noOfMultiples) { // Display multiples System.out.print(" " + num * temp); temp++; } } }
Code language: PHP (php)

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
Code language: JavaScript (javascript)

Explanation:

  • As we asked user to input any value and no of multiples he/she wants to display in for loop program, similarly we have asked user in while loop program also.
  • We have taken a temp variable(int class=”first-color”temp = 1;) to store starting value of multiple and then inside while loop. We will increase the value of temp to find the next multiples in each iteration. Inside while loop we have taken condition temp >= noOfMultiples. So while loop will iterate till the temp value equals to no of multiples given by User.
  • Finally, we have displayed each multiple inside while loop by using System.out.print(” ” + num * i);

Read Also: How To Find Power Of A Number In Java – 3 Simple Ways

Other Readers also ask for:

  1. How to find multiples of a float number in java?
  2. How to find multiples of a number in java?
  3. How to get multiples of float values in Java?

Leave a Comment