Write a Java Program to find Factors of a Number using For Loop, While Loop, Do While Loop, and Functions. The numbers that are completely divisible by the given number (it means the remainder should be 0) called as factors of a given number.
Java Program to Find Factors of a Number Using For Loop
This Java program allows the user to enter any integer value. Next, this program will find Factors of a number using the Java For Loop.
// Java Program to Find factors of a number using For Loop package SimplerPrograms; import java.util.Scanner; public class FactorsOfNumberUsingFor { private static Scanner sc; public static void main(String[] args) { int Number, i; sc = new Scanner(System.in); System.out.println("Please Enter any number to Find Factors: "); Number = sc.nextInt(); for(i = 1; i <= Number; i++) { if(Number%i == 0) { System.out.format(" %d ", i); } } } }
Within this Java Program to Find Factors of a Number, We declared two integer variables Number and i. The following statements will ask the user to enter any positive integer. Then, that number assigned to variable Number.
System.out.println("Please Enter any number to Find Factors: "); Number = sc.nextInt();
In the next line, we have For Loop, and the condition inside the for loop (i <= Number) will make sure that i should not exceed the number. Within the For loop, we used If Condition to check whether the Number % i is equal to zero or not. If the condition is True, then i will be printed as output. Please refer Java If statement article to understand the If else statement.
for(i = 1; i <= Number; i++) { if(Number%i == 0) { System.out.format(" %d ", i); } }
Let us see the working principle of this Java Program to Find Factors of a Number for loop in iteration wise. From the above screenshot, the user entered value: Number = 6.
Java Program to Find Factors of a Number First Iteration
For the first Iteration, Number = 6 and i = 1
- Condition inside the For loop (1 <= 6) is TRUE. So, the compiler will start executing statements inside the For loop
- Within the for loop, we have the If Statement and the condition if (6 % 1 == 0) is TRUE. So, System.out.format(” %d “, i); statement will be printed
- Lastly, i will increment by 1. Please refer to Increment and Decrement Operators in Java article to understand the ++ notation.
Second Iteration
From the first Iteration, the values of i changed as i = 2
- Condition inside the For loop (2 <= 6) is TRUE. So, the compiler will start executing statements inside the For loop
- Within the for loop, the if condition if (6 % 2 == 0) is TRUE. So, System.out.format(” %d “, i); statement will be printed
- Lastly, i will be incremented by 1.
Third Iteration
From the second Iteration, the values of i = 3
- Condition inside the For loop (3 <= 6) is TRUE.
- Within the for loop, the if condition if (6 % 3 == 0) is TRUE so, System.out.format(” %d “, i); statement will be printed
- Lastly, i will increment by 1.
Fourth Iteration of Java Program to Find Factors of a Number
From the third Iteration, the values of i = 4
- Condition inside the For loop (4 <= 6) is TRUE.
- Within the for loop, the if condition if (6 % 4 == 0) is False. So, System.out.format(” %d “, i); statement will not be printed
- Lastly, i will increment by 1.
Fifth Iteration
From the fourth Iteration, i = 5
- Condition inside the For loop (5 <= 6) is TRUE.
- Within the for loop, the if condition if (6 % 5 == 0) is False. So, System.out.format(” %d “, i); statement will not be printed
- Lastly, i will increment by 1.
Sixth Iteration
From the fifth Iteration, the values of i changed as i = 6
- Condition inside the For loop (6 <= 6) is TRUE.
- Within the for loop, the if condition if (6 % 6 == 0) is True. So, System.out.format(” %d “, i); statement will be printed
- Lastly, i will be incremented by 1.
Seventh Iteration
From the sixth Iteration, the values of i = 7 and the condition inside the For loop (7 <= 6) is FALSE. So, the Jcompiler will exit from the For Loop.
Java Program to Find Factors of a Number Using While Loop
This Java program allows the user to enter any integer value. By using this value, this program will find Factors of a number using the Java While Loop.
// Java Program to Find factors of a number using While Loop package SimplerPrograms; import java.util.Scanner; public class FactorsOfNumberUsingWhile { private static Scanner sc; public static void main(String[] args) { int Number, i = 1; sc = new Scanner(System.in); System.out.println("Please Enter any number to Find Factors: "); Number = sc.nextInt(); while(i <= Number) { if(Number % i == 0) { System.out.format(" %d ", i); } i++; } } }
We just replaced the Java For loop in the above example with the While loop. If you do not understand the While Loop, then please refer to Java While Loop.
Please Enter any number to Find Factors:
30
1 2 3 5 6 10 15 30
Java Program to Find Factors of a Number Using Do While Loop
This Java program allows the user to enter any integer value. By using this value, this program will find Factors of a number using Java Do While Loop.
package SimplerPrograms; import java.util.Scanner; public class FactorsOfNumberUsingDoWhile { private static Scanner sc; public static void main(String[] args) { int Number, i = 1; sc = new Scanner(System.in); System.out.println("Please Enter any number to Find Factors: "); Number = sc.nextInt(); do { if(Number%i == 0) { System.out.format(" %d ", i); } i++; } while(i <= Number); } }
We just replaced the While loop in the above example with the Do While loop. If you don’t understand the functionality of the do while Loop, then please refer to Java Do While Loop.
Please Enter any number to Find Factors:
256
1 2 4 8 16 32 64 128 256
Java Program to Find Factors of a Number Using Functions
This Java program allows the user to enter any positive integer value. Then, we are going to pass the User entered value to the Method we created. Within this User defined function, this program will find Factors of a number using the Java For Loop
package SimplerPrograms; import java.util.Scanner; public class FactorsOfNumberUsingMethods { private static Scanner sc; public static void main(String[] args) { int Number; sc = new Scanner(System.in); System.out.println("Please Enter any number to Find Factors: "); Number = sc.nextInt(); FactorsOfNumber(Number); } public static void FactorsOfNumber(int Number) { int i; for (i = 1; i <= Number; i++) { if(Number % i == 0) { System.out.format(" %d ", i); } } } }
Please Enter any number to Find Factors:
640
1 2 4 5 8 10 16 20 32 40 64 80 128 160 320 640
In this Java Factors of a Number example, we are calling the FactorsofNumber(Number) method.
FactorsOfNumber(Number);
When the compiler reaches to FactorsofNumber(Number) line in the main() program, the compiler will immediately jump to below function:
public static void FactorsOfNumber(int Number) {
Comments are closed.