How to Write a program to check it is Peterson number or not in java.
Answers
Answered by
1
Answer:
// C++ program to determine whether the number is
// Peterson number or not
#include <iostream>
using namespace std;
// To quickly find factorial of digits
int fact[10]
= { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };
// Function to check if a number is Peterson
// or not
bool peterson(int n)
{
int num = n, sum = 0;
// stores the sum of factorials of
// each digit of the number.
while (n > 0) {
int digit = n % 10;
sum += fact[digit];
n = n / 10;
}
// Condition check for a number to
// be a Peterson Number
return (sum == num);
}
// Driver Program
int main()
{
int n = 145;
if (peterson(n))
cout << "Yes";
else
cout << "No";
return 0;
}
Explanation:
hope it helps
Similar questions
Science,
21 days ago
Science,
21 days ago
Computer Science,
1 month ago
Science,
1 month ago
Math,
8 months ago
Social Sciences,
8 months ago