Find Factors of a Number in Java using While loop | For | Recursion | Functions
Find factors of a number in java using while loop, for loop, recursion, and functions.
In this article, you will learn how to find factors of a number in java using while loop, for loop, recursion, and functions.
Example-1
The factors of the 60 are: 1 2 3 4 5 6 10 12 15 20 30 60
Example-2
The factors of the 75 are: 1 3 5 15 25 75
What are the factors of a number?
The factors of a number are defined as numbers that divided the original number without leaving any remainder (left reminder = 0).
You should have knowledge of the following topics in java programming to understand these programs:
- Java
java.util.Scanner
package - Java
main()
method - Java
for
loop statement - Java
while
loop statement - Java
System.out.println()
function - Java Functions
- Java Recursion
In this article, we solve this problem in four methods:
Java Program to Find Factors of a Number using While loop
// Java Program to Find Factors of a Number using While loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x, i = 1;
System.out.println("-----Enter the positive integer number-----");
x = in.nextInt();
System.out.print("\nThe factors of the " + x + " are: ");
while (i <= x) {
if (x % i == 0) {
System.out.print(i + " ");
}
++i;
}
System.out.print("\n");
}
}
Output
-----Enter the positive integer number-----
75
The factors of the 75 are: 1 3 5 15 25 75
Java Program to Find Factors of a Number using For loop
// Java Program to Find Factors of a Number using For loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x, i;
System.out.println("-----Enter the positive integer number-----");
x = in.nextInt();
System.out.print("\nThe factors of the " + x + " are: ");
for (i = 1; i <= x; ++i) {
if (x % i == 0) {
System.out.print(i + " ");
}
++i;
}
System.out.print("\n");
}
}
Explanation
In these given programs, we have taken input 75
a random number then applied the while
loop and makes a calculation on this random number.
With Itself reminder zero to find the possible factors of this random number.
The same calculation applied to the second program with for
loop.
Java Program to Find Factors of a Number using Functions
// Java Program to Find Factors of a Number using Functions
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("-----Enter the positive integer number-----");
int x = in.nextInt();
FindFactors(x);
}
// This function will return the factors of input number
public static void FindFactors(int x) {
int i = 1;
System.out.print("\nThe factors of the " + x + " are: ");
while (i <= x) {
if (x % i == 0) {
System.out.print(i + " ");
}
++i;
}
System.out.print("\n");
}
}
Output
-----Enter the positive integer number-----
90
The factors of the 90 are: 1 2 3 5 6 9 10 15 18 30 45 90
Java Program to Find Factors of a Number using Recursion
// Java Program to Find Factors of a Number using Recursion
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("-----Enter the positive integer number-----");
int x = in.nextInt();
System.out.print("\nThe factors of the " + x + " are: ");
FindFactors(x, 1);
System.out.print("\n");
}
// This function will return the factors of input number
// Using the recursion
public static void FindFactors(int x, int i) {
// It will check if the number is less than
if (i <= x) {
if (x % i == 0) {
System.out.print(i + " ");
}
// It will call the function recursively
// To print the next number
FindFactors(x, i + 1);
}
}
}