Hi suvendu, it seems you are trying to convert the result of p/q in string ‘ans’ and then trying to retrieve n digits from the string ans. Although your approach is correct, the datatypes are not built to store 100 digits after the decimal place as the problem constraints demand.
It’s better to do the following:
if we have p=22 and q=7,
then 22/7 would give us 3 i.e. 3 is the integer part of the division.
but we need the decimal part up to n digits,
so we do (22 - 3 x 7) / 7 that would give us the fractional part of the division i.e. 1/7.
The decimal digits after ‘3’ would be contributed by this fraction.
and now to get up to n=10 or 100 digits say , for every iteration we do:
(1 x 10) / 7 = 1 this is the first digit after decimal digit.
add this to sum= sum + 1
next: (1 x 10) -(1 x 7)= 3
store 3 in some variable. then repeat through the same steps again again iterate until n digits.
Each time we are calculating only one digit after decimal place and adding it to sum.
NOTE: Above p/q is the integer division of p and q.
below is the code for ref:
Hope this helps.