-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExoS87.java
More file actions
41 lines (34 loc) · 1.21 KB
/
Copy pathExoS87.java
File metadata and controls
41 lines (34 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package exos.instructions;
import java.util.Scanner;
/**
* Exercice S87 : Nombre fort
* Un nombre fort est un nombre dont la somme de la factorielle des chiffres qui le constitue est égale à ce nombre
* Par exemple : 145 = 1! + 4! + 5! soit 145 = 1 + 24 + 120
* Écrire un programme qui permet de savoir si un nombre est fort
*/
public class ExoS87 {
public static void main(String[] args) {
String[] chiffres;
int nombre;
int somme = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Saisir un nombre entier :");
nombre = scanner.nextInt();
// La saisie est transformée en tableau de chiffres
chiffres = String.valueOf(nombre).split("");
for (String caractere : chiffres) {
int chiffre = Integer.parseInt(caractere);
int factorielle = 1;
for (int j = 1; j <= chiffre; j++) {
factorielle *= j;
}
somme += factorielle;
}
if (somme == nombre) {
System.out.println(nombre + " est un nombre fort");
} else {
System.out.println(nombre + " n'est pas un nombre fort");
}
scanner.close();
}
}