 # Getting WA for this soultion? Project code 2.0 : #FRAKK

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main()

{

``````int P, Q;

int T, N;

char a;

double ans;

scanf("%d", &T);

while (T- -)

{

int sum = 0;

scanf("%d", &N);

scanf("%d %d", &P, &Q);

ans = (double)P / Q;

sprintf(a, "%lf", ans);

int i = 0;

while (a[i] != '.')

{

a[i] = '0';

i++;

}

a[i] = '0';

i = 0;

while (i <= N + 1)

{

sum = sum + (int)a[i] - '0';

i++;

}

printf("%d\n", sum);

}

return 0;
``````

}

1 Like

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:
int d=p/q;
long sum=0;
while(n–){
p=p-(d *q);
p *=10;
d=p/q;
sum+=d;
}
Hope this helps.

1 Like

Thank you