NiTish, Nikhil and Natasha

Two boys, Nitish and Nikhil, are at war over a girl, Natasha. Driven by their feelings, they decided to confess to Natasha. Since both of them were equally dumb, Natasha decided that she would go out with that boy who would successfully find the pattern and thus prove that he is less dumb.

Example:

Input:  n = 100
Output: 121

Approach

C++

#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long n = 100;
    long long x = 1;
    long long remtemp = n;
    if (n < 9)
        cout << n << "\n";
    else
    {
        long long ans = 0;
        while (temp >= 9)
        {
            rem = temp % 9;
            ans += rem * x;
            x = x * 10;
            temp = temp / 9;
        }
        ans += temp * x;
        cout << ans << "\n";
    }
}


No comments:

Post a Comment