
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display Factors of a Number in Java
In this article, we will understand how to display the factors of a number. Factors are numbers that divide the original number without leaving a remainder.
For example, 1, 2, 3, 4, 6, and 12 are factors of 12. If a and b are factors of a number, then a x b is also a factor of the number.
If we multiply 3 and 5, we get 15. We say that 3 and 5 are factors of 15. The largest factor of any number is the number itself, and the smallest factor is 1.
- 1 is a factor of every number.
- So, for example, the largest and smallest factors of 12 are 12 and 1.
Below is a demonstration of the same -
Input: 45 Output: The factors of 45 are: 1 3 5 9 15 45
Finding Factors of a Number using Java
There are the following are various ways to find the factors of a number -
-
Using a for loop
-
Using a while loop
-
Using a do-while loop
-
Using the square root
Using a for loop
A for loop is used to repeat a set of statements multiple times. To find the factorial using a for loop. We need to follow the steps given below -
- START
- Declare two integer values, namely my_input and i.
- Read the required values from the user/ define the values.
- Using a for loop, iterate from 1 to my_input, check if the modulus my_input value and 'i' value leaves a reminder. If no reminder is shown, then it's a factor. Store the value.
- Display the result.
- Stop
Example 1
Here, the integer has been previously defined, and its value is accessed and displayed on the console -
import java.util.Scanner; public class Factors { public static void main(String[] args) { int my_input, i; my_input = 45; System.out.println("The number is defined as " +my_input); System.out.print("The factors of " + my_input + " are: "); for (i = 1; i <= my_input; ++i) { if (my_input % i == 0) { System.out.print(i + " "); } } } }
Output
The number is defined as 45 The factors of 45 are: 1 3 5 9 15 45
Example 2
Here is an example where we accept values from the user -
import java.util.Scanner; public class Factors { public static void main(String[] args) { int my_input, i; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the number : "); my_input = my_scanner.nextInt(); System.out.print("The factors of " + my_input + " are: "); for (i = 1; i <= my_input; ++i) { if (my_input % i == 0) { System.out.print(i + " "); } } } }
Output
Required packages have been imported A reader object has been defined Enter the number : 45 The factors of 45 are: 1 3 5 9 15 45
Using a while loop
Let's see the following steps to find factors of a number using a while loop -
- Declare and initialize a number whose factors you want.
- Initialize a counter variable (e.g., i = 1) to start iterating from 1.
- Use a while loop that will iterate till (i <= number)i is less than or equal to the number.
- Inside the loop, check if the number is divisible by i (i.e., number % i == 0).
- If true, i is a factor and increment the counter after each iteration to check the next number.
- Repeat until all numbers are checked.
Example
In the following example, we are calculating the factorial of a number using the while loop -
public class FactorFind { public static void main(String[] args) { int number = 20; // Define the number int i = 1; System.out.println("Factors of " + number + " are:"); while (i <= number) { if (number % i == 0) { System.out.print(i + " "); } i++; //increment value } } }
This will produce the following output -
Factors of 20 are: 1 2 4 5 10 20
Using a do-while loop
Let's see the following steps to find factors of a number using a do-while loop -
- Declare and initialize a number whose factors you want.
- Initialize a counter variable (e.g., i = 1) to start iterating from 1.
- Use a do-while loop till (i <= number) and check if the number is divisible by i (i.e., number % i == 0).
- If yes, then i is a factor and increment the counter variable after each iteration.
Example
Here we are calculating the factorial of a number using the do-while loop -
public class FactorFind { public static void main(String[] args) { int number = 30; // Define the number int i = 1; System.out.println("Factors of " + number + " are:"); do { if (number % i == 0) { System.out.print(i + " "); } i++; } while (i <= number); } }
Following is the output of the above program -
Factors of 30 are: 1 2 3 5 6 10 15 30
Using the Math.sqrt() Method
Every number has factors that are in pairs; for example, 2 and 50 are both factors of 100 because 2 Ã 50 = 100.
To find all the factors, we can check up to only the square root of the number. It is an optimized solution to find the factors of a number. If a pair has the same number twice (like 10 Ã 10), we only count it once. Let's see the following steps to find factors of a number using a for loop -
- Declare and initialize the number n whose factors you want.
- Use Loop and iterate from 1 to the square root of n only (i.e., i goes from 1 to ?n).
- Check divisibility, if n % i == 0.
- If divisible and if i and n/i are equal (meaning n is a perfect square), print i only once.
- Else, print both i and its corresponding factor n/i.
Example
In the following example, we are calculating the factorial of a number using the sqrt() method -
public class FactorFind { public static void main(String[] args) { // Store the number whose factors are to be found int n = 60; System.out.print("Factors of " + n + " are:"); // Loop from 1 to square root of n for (int i = 1; i <= Math.sqrt(n); i++) { // Check if i is a factor if (n % i == 0) { // If divisors are equal (perfect square), print once if (n / i == i) { System.out.print(" " + i); } else { // Print both factors System.out.print(" " + i + " " + (n / i)); } } } } }
Following is the output of the above program -
Factors of 60 are: 1 60 2 30 3 20 4 15 5 12 6 10