Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package emmett.playground;
- import java.util.Scanner;
- // Very picky but, this is not a good name for a class :)
- // https://www.geeksforgeeks.org/java-naming-conventions/
- public class TP2_1 {
- public static Scanner clavier = new Scanner(System.in);
- public static void main(String args[]) {
- // Print to the terminal first, ask the user for an input
- System.out.print("Entrez un nombre : ");
- // After the above is printed, wait for the user to enter an int
- int nombreSaisi = clavier.nextInt();
- System.out.println("La factorielle de " + nombreSaisi + " est " + factNombre2(nombreSaisi));
- }
- public static int factNombre(int element) {
- int multiplicateur = 1; // * google ma aider, pas sure pourquoi ca fonctionne
- int nombreEntier = element;
- // structure de decision
- for (int i = 1; i <= nombreEntier; i++) {
- multiplicateur = multiplicateur * i;
- }
- return element;
- }
- public static int factNombre2(int nombreEntre) {
- // Start the factorielle/multiplicateur at one because its just easier to multiply by 1
- // then continue through the loop
- int factorielle = 1;
- // For every iteration of the loop,
- // -Take the numer the user entered in the keyboard
- // -Subtract from it, the number of times you've been in the loop already
- // -Multiply by the previous result1
- for (int i = 0; i < nombreEntre; i++) {
- factorielle = (nombreEntre - i) * factorielle;
- }
- return factorielle;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement