Java Program to Count Divisors of an Integer Number

Java program to count divisors of an integer number

//Java program to count divisors of an integer number

import java.util.Scanner;

public class CountDivisors {
  public static void main(String[] args) {
    int number; // to store inputted number
    int divisorCNT; // to store divisor count

    //input an integer positive number
    Scanner SC = new Scanner(System.in);

    while (true) {
      System.out.print("Enter a positive integer: ");
      number = SC.nextInt();

      if (number < 0)
        System.out.println("Please enter positive integer !!!");
      else
        break;
    }

    divisorCNT = 0;

    for (int loop = 1; loop <= number; loop++) {
      if (number % loop == 0)
        divisorCNT++;
    }

    //print result
    System.out.println("Total number of divisors of " + number +
      " is " + divisorCNT);

  } // end main()

} // end class CountDivisors

Output

    
Enter a positive integer: 400
Total number of divisors of 400 is 15
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment