Question

In: Computer Science

Consider the following definition: A positive integer num is called a factorion if it equals to...

Consider the following definition:
A positive integer num is called a factorion if it equals to the sum of the factorials of its digits.
For example, 145 is a factorion because 1! + 4! + 5! = 1 + 24 + 120 = 145.
Write a program that asks the user to enter a positive integer and reports if that number is a
factorion or not.
Reminder: the factorial of a positive integer n, denoted by n!, is the product of all positive
integers less than or equal to n: ?! = ? × (? − 1) × (? − 2) × … × 2 × 1.
For example, 5! = 5 × 4 × 3 × 2 × 1 = 120
Also, the value of 0! is defined as 1
Your program should interact with the user exactly as demonstrated in the following two
executions:
Execution example 1:
Please enter a positive integer:
145
145 is a factorion

Solutions

Expert Solution

Code:

#include<iostream>
using namespace std;
int main()
{
   int n,j;
   cout << "Please enter a positive Integer : ";
   cin >> n; //taking input number from user
   int dummy=n,sum=0; //storing it in dummy variable,initialize sum to zero
   while (n!=0) //iterate loop till the number is equal to zero
       {
       int dig=n%10; //extract each digit from number
       if (dig!=0) //if number!=0 then calculate factorial of that number
           {
           int fact=1;
           for (j=1;j<=dig;j++) //calculate factorial
               fact=fact*j;
           sum=sum+fact; //finally add that factorial value to sum
           }
       else
           sum=sum+1; //if digit is zero then directly add 1 to sum since 0!=1
       n=n/10; //divide n with 10 get the next digits number
       }
   if (dummy==sum) //finally check if the sum and dummy are equal then print below
       cout << sum << " is a factorion" << endl;
   return 0;
}


Code and Output Screenshots:

Note : if you have any queries please post a comment thanks a lot...


Related Solutions

Fill in the blanks to rewrite the following statement: There is a positive integer that is...
Fill in the blanks to rewrite the following statement: There is a positive integer that is less than or equal to every positive integer. (a) There is a positive integer m such that m is . (b) There is a such that every positive integer. (c) There is a positive integer m which satisfies the property that given any positive integer n , m is
What will the following code segments print on the screen? int num = 3; switch (num){...
What will the following code segments print on the screen? int num = 3; switch (num){             case 1:                         System.out.println(“Spring”); case 2:                         System.out.println(“Summer”); case 3:                         System.out.println(“Autumn”); case 4:                         System.out.println(“Winter”); }
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
Prove the following theorem. If n is a positive integer such that n ≡ 2 (mod...
Prove the following theorem. If n is a positive integer such that n ≡ 2 (mod 4) or n ≡ 3 (mod 4), then n is not a perfect square.
Consider an n×n square board, where n is a fixed even positive integer. The board is...
Consider an n×n square board, where n is a fixed even positive integer. The board is divided into n 2 unit squares. We say that two different squares on the board are adjacent if they have a common side. N unit squares on the board are marked in such a way that every unmarked square on the board is adjacent to at least one marked square. Determine the smallest possible value of N.
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence,...
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 114, … etc. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. We define Fib(0)=0,...
Let G be an abelian group and n a fixed positive integer. Prove that the following...
Let G be an abelian group and n a fixed positive integer. Prove that the following sets are subgroups of G. (a) P(G, n) = {gn | g ∈ G}. (b) T(G, n) = {g ∈ G | gn = 1}. (c) Compute P(G, 2) and T(G, 2) if G = C8 × C2. (d) Prove that T(G, 2) is not a subgroup of G = Dn for n ≥ 3 (i.e the statement above is false when G is...
For each positive integer, n, let P({n}) =(1/2^n) . Consider the events A = {n :...
For each positive integer, n, let P({n}) =(1/2^n) . Consider the events A = {n : 1 ≤ n ≤ 10}, B = {n : 1 ≤ n ≤ 20}, and C = {n : 11 ≤ n ≤ 20}. Find (a) P(A), (b) P(B), (c) P(A ∪ B), (d) P(A ∩ B), (e) P(C), and (f) P(B′). Hint: Use the formula for the sum of a geometric series
(True or False) The following function will compile. void print(int num) { return num+1; } Group...
(True or False) The following function will compile. void print(int num) { return num+1; } Group of answer choices True False Flag this Question Question 2 10 pts (True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; } Group of answer...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num ==...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num == 0) return 0; else if (num > 100) return -1; else     return num + tq( num – 1 ); } Group of answer choices: 0 4 -1 10 Q2: Given that values is of type LLNode<Integer> and references a linked list (non-empty) of Integer objects, what does the following code do if invoked as mystery(values)? int mystery(LLNode<Integer> list) {    if (list.getLink() ==...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT