Java Program to Find factors of a number

In this tutorial, we will be going to study factors and how to solve the problem Java Program to find Factors of a number.

Also, we will be dealing with factors in detail in this tutorial.

If you don’t know about what are factors and how to calculate factors of a number then you are at a perfect place to learn and gain knowledge.

Find Factors of a Number in Java

Factors are the numbers which are completely divisible by a given number. Any number may have a factor that is greater than 1.

For example, consider a number 4. When we look for getting its factors, the factors would come out to 1,2, and itself the number 4.

As we see that factors are numbers which when divided by the original number does not leave any remainder.

Initially, create a class named as factors and inside the main block, create a variable named Number to take input of number whose factors are to be Determined.

Take the input of the number through the help of the scanner class of the util package.

Next after this, we will iterate a loop and make a condition check inside loop I <=Number so that number does not over cross it.

Inside the loop, we checked a condition whether the entered number is divisible by me or not, if not move further or else display it as a factor.

Lastly, I will be incremented by 1 each time the loop executes and this process goes until the number itself is checked.

Also after each of iteration, we will be checking and displaying on the basis of the remainder whether it’s a factor or not.

 

Code to find the factors of a number in Java

package factor;

import java.util.Scanner;

public class factor {
  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);
      }
    }
  }
}

After the completion of the above code, we will get the desired output, that is we get factors of any number we want to supply.

Below is a link for the output of the above.

find factors of a number in Java

Also read:

One response to “Java Program to Find factors of a number”

  1. Aishi says:

    It was f*****g awesome …. thanks….so much for your help and support !!!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *