In this project you will contemplate the persistence of a positive non-zero integer n. The persistence of n is computed by multiplying all the digits of n by each other, then repeating this process with the product until a single digit is obtained. The number of repetitions we must perform is the persistence of the integer n.
For example, the number n = 715 has persistence 3. The sequence is 715, 35, 15, 5. Your output should look like:
where the -53 and 715 are user input.
2 Implementation Details
The organization of the code forthis assignment is up to you, but I suggest that you write at least the following two functions:
• int getProductOfDigits( int ); - This function should take in a non-negative in- teger (precondition alert) and return the product of its digits. This requires that the function break a base-10 (decimal) number into its digits.
1
Please give me a non-negative integer: -53
ERROR: Only non-negative integers please.
Please give me a non-negative integer: 715
715 -> 35 -> 15 -> 5
The persistence of 715 is 3.
[code]
#include <iostream>
#include <sstream>
usingnamespace std;
int Persistent(int value)
{
int counter = 0;
while (value > 10)
{
int newvalue = 1;
int tmpvalue = value;
while (tmpvalue > 0)
{
// I get the last digit
int tmp = tmpvalue % 10;
newvalue *= tmp;
// last digit is cut
tmpvalue /= 10;
}
value = newvalue;
cout << "element: " << value << endl;
counter++;
}
return counter;
}
int main()
{
int originalNumber = (715);
cout << "original number: " << originalNumber << endl;
cout << " number of element: " << Persistent(715);
return 0;
}