write a program to count and display divisor of a number by using while loop in java
Answers
import java.util.Scanner;
/**
* Example Class
*/
public class ExampleClass {
/**
* System.out.println utility method
*
* @param value : value to print
*/
static void print(String value) {
System.out.println(value);
}
/**
* Method to find the count of divisors of a number
*
* @param no : number to find the count of divisors
* @return : no return value, just print the result
*/
static void findCountOfDivisors(int no) {
//variable to store the result
int result = 0;
//start a loop and check for each number if it can divide the given number
for (int i = 1; i <= no; i++) {
if (no % i == 0) {
//if the reminder is zero, increment the result variable
result++;
}
}
print("Total number of divisors : " + result);
}
/**
* main method for this class
*/
public static void main(String[] args) {
//variable to get the user input
int no;
//scanner object to get the user input
Scanner scanner = new Scanner(System.in);
print("Enter a number : ");
//read the user-input number
no = scanner.nextInt();
findCountOfDivisors(no);
}
}
Output:-
Enter a number :
365
Total number of divisors : 4