Advertisement
cska1312

take n, multiply the digits until only 1 digit remains and write the number of iterations

Jan 13th, 2023 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.   int a,b,c,d;
  7.   int sum = 1;
  8.   int sum1 = 1;
  9.   int sum2 = 1;
  10.   int sum3 = 1;
  11.   int br = 0;
  12.   int number;
  13.   cin >> number;
  14.   if(number >= 0 && number <= 9)
  15.   {
  16.     cout << br <<  endl;
  17.     return 0;
  18.   }
  19.   while(number > 0)
  20.   {
  21.         a = number % 10;
  22.         sum = sum * a;
  23.         number = number / 10;
  24.     }
  25.   br = br + 1;
  26.   if(sum > 9)
  27.   {
  28.   while(sum > 0)
  29.   {
  30.    b = sum % 10;
  31.    sum1 = sum1 * b;
  32.    sum = sum / 10;
  33.     }
  34.     }else{
  35.     cout << br << endl;
  36.     return 0;
  37.     }
  38.   br = br + 1;
  39.   if(sum1 > 9)
  40.   {
  41.     while(sum1 > 0)
  42.   {
  43.       d = sum1 % 10;
  44.       sum2 = sum2 * d;
  45.       sum1 = sum1 / 10;
  46.     }
  47.   }else{
  48.     cout << br << endl;
  49.     return 0;
  50.   }
  51.    br = br + 1;
  52.   if(sum2 > 9)
  53.   {
  54.     while(sum2 > 0)
  55.       {
  56.         c = sum2 % 10;
  57.         sum3 = sum3 * c;
  58.         sum2 = sum2 / 10;
  59.       }
  60.   }else{
  61.     cout << br << endl;
  62.     return 0;
  63.   }
  64.   br = br + 1;
  65.   cout << br << endl;
  66. }
  67. /*
  68. 39 --> 3 (because 3*9 = 27, 2*7 = 14, 1*4 = 4 and 4 has only one digit)
  69. 999 --> 4 (because 9*9*9 = 729, 7*2*9 = 126, 1*2*6 = 12, and finally 1*2 = 2)
  70. 4 --> 0 (because 4 is already a one-digit number)
  71. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement