Số Strong: Tổng giai thừa các chữ số = chính số đó.
- 145: 1!+4!+5! = 1+24+120 = 145 → Strong.
- 123: 1!+2!+3! = 1+2+6 = 9 ≠ 123 → Không Strong.
Thuật toán
temp = n; sum = 0;
while (n > 0) {
digit = n % 10;
fact = 1;
for (i=1; i<=digit; i++) fact *= i;
sum += fact;
n /= 10;
}
if (sum == temp) -> Strong
Chương trình
#include <stdio.h>
int main() {
int n, temp, sum = 0, digit, fact, i;
printf("Nhap so: "); scanf("%d", &n);
temp = n;
while (n) {
digit = n % 10;
fact = 1;
for (i = 1; i <= digit; i++) fact *= i;
sum += fact;
n /= 10;
}
if (sum == temp) printf("%d la so Strong", temp);
else printf("%d khong phai so Strong", temp);
return 0;
}
Kết quả
Run 1: Nhap: 145 -> 145 la so Strong Run 2: Nhap: 25 -> 25 khong phai so Strong