programa Java para contar divisores de un número entero.

  • Inicio
  • Java Resuelto Programas
  • Core Ejemplo de Java Programas

Conde divisores de un usando programa Java Entero

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

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


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *