Mission - Baby

Baby loves to play with numbers. So, here he has a number N. He finds the factorial of N. Then he rotates the digits of Factorial(N) by Z places and get number X. Then finds the Digest sum of the X and add the number of digits in the factorial of N.

Suppose, he has numbers N=5 and Z=2. The Factorial of 5 is 120. Rotating the factorial by 2 places will give X=201. Now the digest sum is the sum of a number recursively until it is less than 10. So, digest sum is 3 and the number of digits in the factorial of N=5 is 3. So, the output should be 3+3=6.

Example:

Input:  n = 5, z = 2
Output: 6

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n = 5z = 2;
    float x = 1;
    for (int i = 2i <= ni++)
    {
        x = x + log10(i);
    }
    int h = x;
    if (n == 1)
        cout << 2;
    else if (n == 2)
        cout << 3;
    else if (n == 3)
        cout << 7;
    else if (n == 4)
        cout << 8;
    else if (n == 5)
        cout << 6;
    else
        cout << (9 + h);
    return 0;
}


No comments:

Post a Comment