Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. package emmett.playground;
  2.  
  3. import java.util.Scanner;
  4.  
  5. // Very picky but, this is not a good name for a class :)
  6. // https://www.geeksforgeeks.org/java-naming-conventions/
  7. public class TP2_1 {
  8.  
  9. public static Scanner clavier = new Scanner(System.in);
  10.  
  11. public static void main(String args[]) {
  12. // Print to the terminal first, ask the user for an input
  13. System.out.print("Entrez un nombre : ");
  14. // After the above is printed, wait for the user to enter an int
  15. int nombreSaisi = clavier.nextInt();
  16. System.out.println("La factorielle de " + nombreSaisi + " est " + factNombre2(nombreSaisi));
  17. }
  18.  
  19. public static int factNombre(int element) {
  20. int multiplicateur = 1; // * google ma aider, pas sure pourquoi ca fonctionne
  21. int nombreEntier = element;
  22. // structure de decision
  23. for (int i = 1; i <= nombreEntier; i++) {
  24. multiplicateur = multiplicateur * i;
  25. }
  26. return element;
  27. }
  28.  
  29. public static int factNombre2(int nombreEntre) {
  30.  
  31. // Start the factorielle/multiplicateur at one because its just easier to multiply by 1
  32. // then continue through the loop
  33. int factorielle = 1;
  34. // For every iteration of the loop,
  35. // -Take the numer the user entered in the keyboard
  36. // -Subtract from it, the number of times you've been in the loop already
  37. // -Multiply by the previous result1
  38. for (int i = 0; i < nombreEntre; i++) {
  39. factorielle = (nombreEntre - i) * factorielle;
  40. }
  41. return factorielle;
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement