elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Estamos en la red social de Mastodon


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Factorial
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Factorial  (Leído 2,366 veces)
Kougami

Desconectado Desconectado

Mensajes: 53


Ver Perfil
Factorial
« en: 6 Octubre 2017, 17:52 pm »

Buenas, tengo un problema con el siguiente codigo:

Código
  1. package factorial;
  2.  
  3. public class Factorial {
  4.  
  5. public static int factorial (int n) {
  6. int fact = 1;
  7. if (n < 0) {
  8. fact = 0;
  9. }
  10. else if ( n == 0){
  11. fact = 1;
  12. }
  13. else {
  14. fact = fact * n;
  15. }
  16. return fact;
  17. }
  18.  
  19. public static int combinatorio (int n, int k) {
  20. int combi = factorial (n) / (factorial(k) * factorial(n-k));
  21. return combi;
  22. }
  23.  
  24. public static void main(java.lang.String[] args) {
  25. for (int i = 0; i < 6; ++i) {
  26. for (int j = 0; j <= i; ++j)
  27. System.out.print(Factorial.combinatorio(i,j) + " ");
  28. System.out.println();
  29. }
  30. }
  31. }
  32.  

La salida deberia ser:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Pero sin embargo me sale:
1
1 1
1 2 1
1 1 1 1
1 1 1 1 1
1 1 0 0 1 1

Para compilar y ejecutar estoy usando el Eclipse 4.4 sobre java 1.8.0
Muchas gracias de antemano


En línea

ivancea96


Desconectado Desconectado

Mensajes: 3.412


ASMático


Ver Perfil WWW
Re: Factorial
« Respuesta #1 en: 6 Octubre 2017, 21:54 pm »

Ve por partes. Primero, asegurate de que cada función realiza su cometido correctamente.
¿La función factorial lo calcula correctamente?
Para n=5, el factorial lo estás calculando así: fact = 1(fact) * 5(n) -> 5.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Ahora el factorial con while, wend.
Programación Visual Basic
TiT@n 3 1,297 Último mensaje 11 Diciembre 2006, 19:44 pm
por CeLaYa
[C]Factorial de un numero
Programación C/C++
HRSLASH 5 5,429 Último mensaje 9 Agosto 2010, 23:04 pm
por leogtz
Problema con Factorial.. « 1 2 »
Programación C/C++
Komodo 10 5,189 Último mensaje 6 Septiembre 2010, 11:00 am
por Komodo
Factorial de un numero!
Programación C/C++
barnix456 3 3,105 Último mensaje 12 Mayo 2012, 17:00 pm
por barnix456
Factorial c++
Programación C/C++
exanime 1 1,798 Último mensaje 8 Septiembre 2014, 21:11 pm
por Yaldabaot
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines