Java Online Compiler
Example: Count Numbers with Exactly 9 Divisors (Brute Force) in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Count Numbers with Exactly 9 Divisors (Brute Force) import java.util.Scanner; // Main class containing the entry point of the program public class Main { // Function to count divisors of a number public static int countDivisors(int n) { int count = 0; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { if (i * i == n) { count++; } else { count += 2; } } } return count; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the upper limit (N): "); int N = scanner.nextInt(); scanner.close(); int numbersWithNineDivisors = 0; // Step 1: Iterate through each number from 1 to N for (int i = 1; i <= N; i++) { // Step 2: Count divisors for the current number int divisors = countDivisors(i); // Step 3: Check if the count is exactly 9 if (divisors == 9) { numbersWithNineDivisors++; System.out.println(i + " has exactly 9 divisors."); } } System.out.println("Total numbers up to " + N + " with exactly 9 divisors: " + numbersWithNineDivisors); } }
Output
Clear
ADVERTISEMENTS