1
resposta

Ao Infinity e além

package br.com.alura.screenmatch.loop;

import java.util.Scanner;

public class Fatorial {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Digite um número");
        float numero = scanner.nextFloat();
        
        float fatorial = 1;
        
        for (int i = 1; i <= numero; i++) {
            fatorial *= i;
            System.out.println("O fatorial de " + i + " é " + fatorial);
        }
        System.out.println("---------------------------------------------");
        System.out.println("O fatorial de " + numero + " é " + fatorial);
        scanner.close();
    }

}

Digite um número
50
O fatorial de 1 é 1.0
O fatorial de 2 é 2.0
O fatorial de 3 é 6.0
O fatorial de 4 é 24.0
O fatorial de 5 é 120.0
O fatorial de 6 é 720.0
O fatorial de 7 é 5040.0
O fatorial de 8 é 40320.0
O fatorial de 9 é 362880.0
O fatorial de 10 é 3628800.0
O fatorial de 11 é 3.99168E7
O fatorial de 12 é 4.790016E8
O fatorial de 13 é 6.227021E9
O fatorial de 14 é 8.717829E10
O fatorial de 15 é 1.3076743E12
O fatorial de 16 é 2.0922788E13
O fatorial de 17 é 3.556874E14
O fatorial de 18 é 6.4023735E15
O fatorial de 19 é 1.216451E17
O fatorial de 20 é 2.432902E18
O fatorial de 21 é 5.109094E19
O fatorial de 22 é 1.1240007E21
O fatorial de 23 é 2.5852017E22
O fatorial de 24 é 6.2044845E23
O fatorial de 25 é 1.5511211E25
O fatorial de 26 é 4.032915E26
O fatorial de 27 é 1.088887E28
O fatorial de 28 é 3.0488837E29
O fatorial de 29 é 8.841763E30
O fatorial de 30 é 2.6525289E32
O fatorial de 31 é 8.2228397E33
O fatorial de 32 é 2.6313087E35
O fatorial de 33 é 8.6833185E36
O fatorial de 34 é 2.9523282E38
O fatorial de 35 é Infinity
O fatorial de 36 é Infinity
O fatorial de 37 é Infinity
O fatorial de 38 é Infinity
O fatorial de 39 é Infinity
O fatorial de 40 é Infinity
O fatorial de 41 é Infinity
O fatorial de 42 é Infinity
O fatorial de 43 é Infinity
O fatorial de 44 é Infinity
O fatorial de 45 é Infinity
O fatorial de 46 é Infinity
O fatorial de 47 é Infinity
O fatorial de 48 é Infinity
O fatorial de 49 é Infinity
O fatorial de 50 é Infinity

O fatorial de 50.0 é Infinity

1 resposta

Oi, Roberto! Como vai?

Gostei de como você utilizou o for para calcular o fatorial passo a passo e ainda exibiu o resultado parcial a cada iteração. Isso ajuda bastante a visualizar o crescimento do valor. O Infinity aconteceu porque o tipo float tem limite de armazenamento, e o fatorial cresce muito rápido, ultrapassando esse limite.

Uma dica interessante para o futuro é utilizar o tipo BigInteger, que suporta números muito grandes. Veja este exemplo:


import java.math.BigInteger;

BigInteger fatorial = BigInteger.ONE;

for (int i = 1; i <= 50; i++) {
    fatorial = fatorial.multiply(BigInteger.valueOf(i));
}

System.out.println(fatorial);

Esse código usa BigInteger para calcular o fatorial sem perder precisão, mesmo com números grandes.

Alura Conte com o apoio da comunidade Alura na sua jornada. Abraços e bons estudos!