Vu mon code:factoriel boucle For ne fonctionne que jusqu'à 12
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Fact_2 {
public static void main(String args[]) throws IOException {
System.out.println("Please enter a number:");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int fact = Integer.parseInt(input.readLine());
int factorial = 1;
for (int i = 1; i <= fact; i++) {
factorial = factorial * i;
}
System.out.println("The factorial of " + fact + " is " + factorial);
}
}
Le programme fonctionne correctement ... que jusqu'au 12 chiffres. J'ai vérifié pour m'assurer que toutes les factorielles étaient correctes mais quand vous entrez 13 pour votre numéro vous obtenez 1932053504 quand il devrait être 6227020800. Pourquoi est-ce?
Lire sur le dépassement d'entier. Puis retournez et changez 'int factorial = 1;' 'long factorial = 1;' –
@DavidWallace merci! – feelingstoned