In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C.
What is Peterson Number?
A number is said to be a Peterson number if the sum of factorials of each digit of the number is equal to the number itself.
Peterson numbers examples:
Number = 145
145 = !1 + !4 + !5
= 1 + 4*3*2*1 + 5*4*3*2*1
= 1 + 24 + 120
= 145 = 145
Therefore, we can see that after the sum of the factorial of 145 is equal to the number 145 itself. Hence the Peterson Number.
C Program to check whether the number is Peterson Number or not
Peterson number program in C:
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 | #include <stdio.h> int main() { int num, temp, rem, sum = 0, fact = 1; int i; printf("Enter a number: "); scanf("%d", &num); temp = num; while (temp != 0) { rem = temp % 10; for (i = 1; i <= rem; i++) fact *= i; sum += fact; fact = 1; temp /= 10; } //display if (num == sum) printf("%d is a Peterson Number", num); else printf("%d is Not a Peterson Number", num); return 0; } |
Output 1:
Enter a number: 145
145 is a Peterson number
Output 2:
Enter a number: 55
55 is not a Peterson number