×

Java Programs

Java Practice

Java program to count divisors of an integer number

Problem statement

This program will read an integer number and print total number of divisors of entered integer number.

Count Divisors of an Integer using Java program

//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

Core Java Example Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.