Find out the divisors or the factors of any number using java program

Program to display the Factors of any number


"Factors" are the numbers you multiply to get another number. For instance, factors of 20 are 4 and 5, because 4×5 = 20. Some numbers have more than one factorization (more than one way of being factored). For instance, 10 can be factored as 1×10, or 2×5.
For Example:-
The factors of 100
Answer : 1,2,4,5,10,20,25,50,100.


import java.util.Scanner//Calling Scanner Class
public class Divisors
{
    // instance variable 
    private int x;
    public void calculation() //Non return type, Non Parameterized method
    {
        System.out.println("Enter any number"); //Instruction to user
        xnew Scanner(System.in).nextInt()//Getting input
        System.out.println("The divisors of "+x+" are:"); //Output
        int a=1; //Declaring and initializing the variable
        while(a<=x//Loop the program until a is less than or equals to x
        {
            if(x%a==0) //Conditon
            {
                System.out.println(a); //Output
            }
            a++; //Using Increment Operator
        }
    }
    public static void main(String []args) //Main method
    {
        Divisors d= new Divisors(); //Creating object of Divisors class
        d.calculation()//Calling method
    }
}

If you need some data from the user then you need to import the Scanner class. If you want to know more about Scanner class then, please visit this link (https://programmerbrothers.blogspot.com/2019/04/scanner-class-in-java-scanner-is-class.html)


Output:-


If you have any problem regards Increment operator then, this site will definitely help to know well about it.

Comments

Popular posts from this blog

Java Program to make a comparison between numbers

Java program using Scanner Class

Java program to print 'Hello World'