Posted by : Unknown
Thursday, 18 June 2015
import java.util.Scanner;
class CountDivisors
class CountDivisors
{
public static void main(String[] args)
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
int N,testDivisor,divisorCount,numberTested;
while (true)
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++)
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)
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);
}
}
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
The number of divisors of 5 is 2