In Java, to print all factors of a number we can use loop concepts such as while loop or for loop. For a given number, we should find all the factors (which are exactly divide that number) of that number as output. If the input is 6, then the output should be 1,2,3 and 6. If the input is a negative number -6, then the output should be -6,-3,-2,-1,1,2,3 and 6.
In this article we will learn through an example, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators (Ternary operator)
- Loop concepts
Problem Statement
We have to write a Java program to print all factors of a positive and negative number. For a given number (positive or negative), the program should find all the factors of the number. This will be done in our program. Let’s see an example,
Example 1:
For a negative number -4, output should be -4 -2 -1 1 2 4
Explanation:
Input integer negative number: -4
Factors of the negative number: -4 -2 -1 1 2 4
We will get all the negative and absolute values |x| (positive) of the number -4.
Example 2:
For a number 4, output should be 1 2 4
Explanation:
Input integer number: 4
Factors of the number: 1 2 4
We will get all the factors (1 2 4) of the number 4.
Logic to print all factors of a number
There are multiple logics to print all factors of a number in the java programming language.
Logic 1: For positive number
- Take an input number from the user.
- Initialize a variable i to 1. This will be used to iterate from 1 to the given number to find the factors.
- A while loop is used to iterate from 1 to the value of number (i<=number). Inside the loop, using if condition, ‘num%i==0’ checks if i is a factor of number.
- If the remainder is 0, it means i is a factor of number, so i is printed. The loop variable i is incremented by 1 after each iteration.
- After completing the loop, the program will closes the scanner class and exit.
Logic 2: For both negative and positive number
- Take an input number from the user.
- Use a for loop to iterate through potential factors of the given number. The loop condition dynamically adjusts based on the sign of number.
- Using ternary operator, if number is positive, the loop starts from i=1. If number is negative, the loop starts from i= -Math.abs(number), ensuring negative factors are included.
- Inside the loop, using if else condition, it checks if the value of i is 0. If it is zero, the program uses a continue statement to skip the current iteration.
- Otherwise, using the condition, ‘num%i==0’ checks if i is a factor of number. If i is found to be a factor of number, the program prints the value of i.
- After completing the loop, the program will exit and closes the scanner class.
Java program to print all factors of a given number
Program 1: For Positive number
In the below program we are using while loop to print all the factors of a given number in Java.
import java.util.Scanner;
public class Factors {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
int number = s.nextInt();
int i = 1;
System.out.println("Factors of number are:");
while (i <=number) {
if (number % i == 0) {
System.out.println(i);
}
i++;
}
s.close();
}
}
Output:
Enter a number
10
Factors of number are:
1
2
5
10
Java program explanation to print all factors of a given positive number
- The program asks the user to enter a number.
- Read the number using a
Scanner
and store it in the variablenumber
. - Initialize
i
to 1. - Print “Factors of number are:” to indicate output.
- Enter a
while
loop that continues as long asi
is less than or equal tonumber
. - Check if
number % i == 0
to determine ifi
is a factor ofnumber
. - Print
i
if it is a factor. - Increment
i
after each iteration. - Close the
Scanner
objects
after finishing to release resources.
Program 2: For Negative and positive number
In the below program we are using for loop to print all the factors for both negative number and positive number in Java.
import java.util.Scanner;
public class Factors {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
int number = s.nextInt();
System.out.println("Factors of number are:");
// Loop for handling both positive and negative numbers
for (int i = (number > 0) ? 1 : -Math.abs(number); i <= Math.abs(number); i++) {
if (i == 0) {
continue; // skip iteration for 0
}
if (number % i == 0) {
System.out.println(i);
}
}
s.close();
}
}
Output:
Enter a number
-2
Factors of number are:
-2
-1
1
2
Java program explanation to print all factors of a given negative number
- The Java program ask the user to enter a number.
- Read and store the number using a
Scanner
. - Determine the loop range based on whether
number
is positive or negative. - Iterate through potential factors from either 1 to
number
(if positive) or from-Math.abs(number)
toMath.abs(number)
(if negative). - Check if each
i
is a factor ofnumber
usingnumber % i == 0
. - Skip iteration if
i
is zero to avoid division by zero. - Print each factor found during the iteration.
- Close the
Scanner
object to release system resources after finishing.
Conclusion
In this article, we have learnt how to print all factors for positive and negative number. Here, we have seen the logic to print only factors for positive numbers and for both positive and negative numbers and then moved to writing the program. Hope this article helped you in the understanding of the program.
You can also check our another awesome tutorials
- Java program check perfect number
- The concept of ternary operator
- Program to swap two values using third variable in Java
- Java program to calculate power without using pow method
Happy Coding!