ADVERTISEMENT
ADVERTISEMENT

Java program to count divisors of an integer number

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 »


ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.