Skip to content

Instantly share code, notes, and snippets.

@oscarcpozas
Created January 16, 2017 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarcpozas/9bf95b99256226162aef676e55aaa853 to your computer and use it in GitHub Desktop.
Save oscarcpozas/9bf95b99256226162aef676e55aaa853 to your computer and use it in GitHub Desktop.
Resuelto 02
import java.io.*;
public class Factorial {
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
int num;
int factorial = 1;
System.out.println("Por favor introduzca un número del 1 al 7:");
boolean valido = false;
do {
num = Integer.parseInt(buffer.readLine());
if(num <= 100 && num >= 1) {
valido = true;
} else {
System.out.println("El numero introducido no es valido");
}
} while (!valido);
int numeroIntroducido = num; // Esto es una tonteria, para el system.out del final
while (num != 0) {
factorial = factorial * num; // Esto se podria abreviar a: factorial *= num;
num --;
}
System.out.println("El factorial de " + numeroIntroducido + " es " + factorial);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment