Posted by : Unknown Thursday, 18 June 2015


import java.util.Scanner;
class CountDivisors 
{
    public static void main(String[] args) 
   {
        Scanner input = new Scanner( System.in );
        int N,testDivisor,divisorCount,numberTested;
        while (true) 
        {
            System.out.print("Enter a positive integer: ");
            N = input.nextInt();
            if (N > 0)
                break;
            System.out.println("That number is not positive.  Please try again.");
        }
        divisorCount = 0;
        numberTested = 0;
        for (testDivisor = 1; testDivisor <= N; testDivisor++) 
       {
            if ( N % testDivisor == 0 )
                divisorCount++;
            numberTested++;
            if (numberTested == 1000000) 
            {
                System.out.print('.');
                numberTested = 0;
            }
        }
        System.out.println();
        System.out.println("The number of divisors of " +N+ " is " + divisorCount);
    }
}

Output:

Enter a positive integer: 5

The number of divisors of 5 is 2

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Welcome to My Blog

Translate

Popular Post

Total Pageviews

Blog Archive

- Copyright © Learning Java Program - Powered by Blogger -