Java Program to Display Factors of a Number

You are currently viewing Java Program to Display Factors of a Number

Hey Guys! Are you looking to know the concept of the Java Program to Display Factors of a Number? If yes then you are in the perfect article. Initially, I would like to explain the factors of a number and how to execute a task here?

What is factors of a number?

It is nothing but a number that is perfectly divisible by the original number with zero remainder.

For example, If we can select an input number that is “16” then the factors of “16” will be like “1, 2, 4, 8, and 16”. Okay, Let’s start the article.

Java Program to Display Factors of a Number

Below, We can find the factors of a positive number using this java program.

public class Main {

  public static void main(String[] args) {

    // positive number
    int number = 50;

    System.out.print("Factors of " + number + " are: ");

    // loop runs from 1 to 60
    for (int i = 1; i <= number; ++i) {

      // if number is divided by i
      // i is the factor
      if (number % i == 0) {
        System.out.print(i + " ");
      }
    }
  }
}

Explantion

Initially, We have to assign the input number with the respective variable. Secondly, The for loop iterates from 1 to the input number. Thirdly, The if…condition will use to find the numbers that can be perfectly divisible by the same input number.

Finally, Those numbers will print on the result screen using the system.out.printIn() function.

Output

Below, I can provide the reference output of the java program to display factors of positive numbers.

Factors of 50 are: 1 2 5 10 25 50
Read Also : Find the Factors of a Number in JavaScript

Java Program to Display Factors of a Number

Here, You can see the example program to find the factors of a negative number.

class Main {

  public static void main(String[] args) {

    // negative number
    int number = -30;
    System.out.print("Factors of " + number + " are: ");

    // run loop from -60 to 60
    for(int i = number; i <= Math.abs(number); ++i) {

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

Execution

After declaring the inputs and variables, The for loop along with the Math.abs() function iterates from the negative input number to the same positive(absolute) number.

Later, We will find the factors of a negative input number using the if…else condition. Finally, The output will display on the screen using the system.out.printIn() function.

Output

This is the reference output of factors of a negative number.

Factors of -30 are: -30 -15 -10 -6 -5 -3 -2 -1 1 2 3 5 6 10 15 30

Final Thoughts

We hope this article helps you to learn Java Program to Display Factors of a Number will be useful to you. If you face any issues regarding this study? ask us via the comment section. Do share our articles with other Java developers and learners through social media.

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply