Java Program to Display Factors of a Number

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Program to Display Factors of a Number

In this article, we will learn multiple ways to display the factors of a number.

A factor is defined as the number which is perfectly divisible by other number. Now, our task is to display all the factors of a number.

We will see different approaches to get achieve it.

Let’s see different methods on by one.

Method:-I – Java program to display factors of positive number using for loop

Factors of a positive number can be displayed by taking a for loop. The for loop will iterate from 1 till the number, wherever number is perfectly divisibly by iterative value, print the iterative value. The values obtained are the factors of the number.

Approach:

  • Declare an int variable say num and store a positive number.
  • Take a for loop and iterate variable say from 1 till num.
  • Wherever num%i==0, print i.
  • The i values are the factors of num.

Program:

Java program to display factors of positive number using for loop

class Main 
{

  public static void main(String[] args) 
{

    // A positive number
    int num = 100;

    System.out.print("Factors of " + num + " = ");

    // loop iterates from 1 to num
    for (int i = 1; i <= num; ++i) {

      // if num is divisibly by i, print i
      if (num % i == 0) {
        System.out.print(i + " ");
      }
    }
  }
}
Output:

Factors of 100 = 1 2 4 5 10 20 25 50 100

Method:-II – Java program to display factors of positive number using user-defined input

java.util package provide Scanner class which enables user to give inputs.

In this method we will display the factors in the similar was as that of the Method-I, but here we have only to take the number as user input.

Approach:

  • Import Scanner class from java.util package.
  • Make instance of Scanner class.
  • Ask user to enter the input and store it in variable say num.
  • Take a for loop and iterate variable say from 1 till num.
  • Wherever num%i==0, print i.
  • The i values are the factors of num.

Program:

Java program to display factors of positive number

import java.util.Scanner;
class Main 
{

  public static void main(String[] args) 
  {    
    // A positive number
    int num;  
    
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter a number : ");
    num=sc.nextInt();

    System.out.print("Factors of " + num + " = ");

    // loop iterates from 1 to num
    for (int i = 1; i <= num; ++i) {

      // if num is divisibly by i, print i
      if (num % i == 0) {
        System.out.print(i + " ");
      }
    }
  }
}
Output:
Enter a number :
100
Factors of 100 = 1 2 4 5 10 20 25 50 100

Method:-III – Java program to display factors of negative number using for loop

Math.abs in java returns the absolute value (positive value) of a number.

Since negative numbers have not only negative factors but also positive factors. So by the help of for loop we will iterate from negative of that number till the positive value of the number. If iterative value=0, we will skip the iteration otherwise wherever iterative value is perfectly divisible, we will display them.

Approach:

  • Declare a negative number and store in an integer variable num.
  • Iterate loop from -num to +num (as factors of a negative number is also positive number).
  • If i becomes, skip the iteration
  • Else, if num%i==0 print i.
  • i is the factors of number.

Program:

Java program to display factors of negative number using for loop

class Main 
{

  public static void main(String[] args) 
  {

    // A negative number
    int num = -50;
    System.out.print("Factors of " + num + " = ");

    // Loop will iterate from -50 to -50
    for(int i = num; i <= Math.abs(num); ++i) {

      // if i=0 skip the iteration
      if(i == 0) {
        continue;
      }
      else {
        if (num % i == 0) {
          System.out.print(i + " ");
        }
      }
    }
  }
}

Output:

Factors of -50 = -50 -25 -10 -5 -2 -1 1 2 5 10 25 50

Method:-IV – Java program to display factors of number using while loop

We can display the factors of a number using do while loop. The while body will iterate from 1 till the number. It prints the numbers wherever the number is divisible by iterative value. These displayed numbers are the factors of the number.

Approach:

  • Declare num and i.
  • Initialize num to 100 and i to 1.
  • Take a variable say i.
  • With the help of while loop iterate i till num
  • In while loop if i%2==0, print i.
  • Increment i.

Program:

Java program to display factors of number using while loop

class Main 
{

  public static void main(String[] args) 
  {
      
        
    // A positive number
    int num=100, i=1;  

    System.out.print("Factors of " + num + " = ");
    
    
    // iterate i from 1 to num
    while(i <= num) {
        // if num is divisible by i print i
            if(num % i == 0) {
                System.out.print(i + " ");
            }
            i++;
        }
  }
}

Output

Factors of 100 = 1 2 4 5 10 20 25 50 100

Method:-V – Java program to display factors of number do-while loop

We can display the factors of a number using do while loop. In the do body we will if number is perfectly then we will print the numbers. Outside the do body in the while loop condition will ensure that iteration will occur only till the number.

Approach:

  • Declare num and i.
  • Initialize num to 100 and i to 1.
  • In DO body execute the following.
  • If num%i==0, print i.
  • Increment i
  • Come out of do body.
  • Taking a while loop check if i<num

Program:

Java program to display factors of number do-while loop

class Main 
{

  public static void main(String[] args) 
  {
      
        
    // A positive number
    int num=100, i=1;  

    System.out.print("Factors of " + num + " = ");
    
    // check if num perfectly divisible by i
    // if yes print i
    // after do body execution, check in while condition if i<=num
    do {
        if(num % i == 0) 
        System.out.print(i+" ");
        i++;
    } while (i <= num);
  }
  
}

Output:

Factors of 100 = 1 2 4 5 10 20 25 50 100

Method:-VI – Java program to display factors of number using recursion

In this approach we will display the factors of a number using recursion. From the main method we will pass the number as arguments and a iterative variable to a method say factRecur.

The recursion method will not only check and print the factors, but also will increment the iterative variable and will again call the recursion method. This process will continue till iterative variable reaches num.

Approach

  • Create a recursive method which accepts number and i value from main method
  • In the method execute the following.
  • Check if i<=number
  • Now check if the number is divisible by i, if yes print i.
  • Then by calling recursive method pass the number and i+1 as arguments.
  • The method will recursively call itself till i becomes equal to the number.
  • In main method declare and initialize num as 100. (Pass any number you want to see factors)
  • Take the recursive method pass the number and 1 as arguments.

Program:

Java Program to display Factors of Number using Recursion

class Main 
{

   public static void factRecur(int no, int i) 
   {

      // check i less than number
      if(i <= no) 
      {

          // check if no is divisible by i
          if(no%i == 0)
          System.out.print(i+" ");

          // recursive call to pass next number
          factRecur(no, i+1);

       } 

   }

   public static void main(String[] args) 
   {

       // declare variable
       int num = 100;

       // Print the factors of a number
       System.out.print("The factors = ");
       factRecur(num, 1);

     }
}
Output:

The factors = 1 2 4 5 10 20 25 50 100

Related Java Decision Making and Loop Programs: