# Java: Accepts an integer and count the factors of the number

## Java Basic: Exercise-57 with Solution

Write a Java program to accept an integer and count the factors of the number.

**Sample Solution:**

**Java Code:**

```
import java.util.*;
public class Exercise57 {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner in = new Scanner(System.in);
System.out.print("Input an integer: ");
// Read an integer from the user
int x = in.nextInt();
// Call the result method and print the result
System.out.println(result(x));
}
// Define a method to calculate the number of divisors for a given integer
public static int result(int num) {
int ctr = 0;
// Iterate from 1 to the square root of the input number
for (int i = 1; i <= (int) Math.sqrt(num); i++) {
// Check if 'i' is a divisor, and if it's not a perfect square
if (num % i == 0 && i * i != num) {
ctr += 2; // Increase the count by 2
} else if (i * i == num) {
ctr++; // If 'i' is a perfect square, increase the count by 1
}
}
return ctr; // Return the total count of divisors
}
}
```

Sample Output:

Input an integer: 25 3

**Pictorial Presentation: **

**Flowchart: **

**Java Code Editor:**

**Previous:** Write a Java program to find the number of values in a given range divisible by a given value.

**Next:** Write a Java program to capitalize the first letter of each word in a sentence.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.**

https://www.w3resource.com/java-exercises/basic/java-basic-exercise-57.php

**Weekly Trends and Language Statistics**- Weekly Trends and Language Statistics