Broken calculator ,wrong answer

@aryanv Your logic is not working correctly, dry run with any test case. It is always giving wrong answer.
I am giving you a hint to approach this problem;
Consider the given number is n:
Initially compute (n)(n-1) since even for large n say 500, it will not be too large and can be stored in a variable.
500
499=249500
Now extract each digit of this and store it in a vector/array in reverse order. So… 0 in 0th position,0 in 1st pos,5 in 2nd position, 9 in 3rd position and so on.
Now iterate from n-2 to 1 and multiply and expand the vector/array as below:
int mulfac,carry;
for(int i=n-2;i>=1;i–)
{
carry=0;
for(int j=0;j<counter;j++)
{
mulfac=(i*arr[j])+carry;
arr[j]=mulfac%10;
carry=mulfac/10;
}
if(carry>9){
initial=carry;
while(initial!=0)
{
rem=initial%10;
arr[counter++]=rem;
initial=initial/10;
}
}
if((carry>0)&&(carry<10))
arr[counter++]=carry;
}
At the end of the iteration, you will have the result stored in the vector in reverse order.

Hope this helps :slightly_smiling_face:

1 Like