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
Convert this code written in Python to Java: # Definition of a function isprime(). def isprime(num):...
Convert this code written in Python to Java: # Definition of a function isprime(). def isprime(num):     count=2;     flag=0;     # Loop to check the divisors of a number.     while(count<num and flag==0):         if(num%count!=0):             # Put flag=0 if the number has no divisor.             flag=0         else:             # Put flag=1 if the number has divisor.             flag=1         # Increment the count.         count=count+1     # Return flag value.     return flag # Intialize list. list=[]...
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.
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”); }
5C++ Questions 1. What will the following code print? num = 8; cout << --num <<...
5C++ Questions 1. What will the following code print? num = 8; cout << --num << " "; cout << num++ << " "; cout << num; 2.Since the while loop evaluates the condition before executing the statement(s), it is known as a(n)____ loop, whereas the do-while loop evaluates the condition after the statements have been executed, it is known as a(n)____ loop. 3.T/F While loops can only be used when a counter is decremented to zero. If the counter...
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...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT