Find out the divisors or the factors of any number using java program
Program to display the Factors of any number
For Example:-
The factors of 100
Answer : 1,2,4,5,10,20,25,50,100.
import java.util.Scanner; //Calling Scanner Classpublic class Divisors{// instance variableprivate int x;public void calculation() //Non return type, Non Parameterized method{System.out.println("Enter any number"); //Instruction to userx= new Scanner(System.in).nextInt(); //Getting inputSystem.out.println("The divisors of "+x+" are:"); //Outputint a=1; //Declaring and initializing the variablewhile(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 classd.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
Post a Comment