This program will take 3 interger values a, b and N.
The program will find the sum of the decimals from division value of (a/b) up to N numbers.
For example:
Sorry my technical english wasn't very good, my program (in C) should do the following:
- It takes values of a, b, and N
- It perform division of a and b
- It takes the division in float and separate it into integer value and fractional part
- It add to sum of fractional part rounded down
- It update the base division value by multiply by 10
- It update conditions of the loop of n=N
- Output in printf.
However, my program only give the right value up till a=252525 b=99 N=33 output=171 which i think is wrong. Please help me correct my algorithm.
#include<stdio.h>
int main()
{
unsigned int a, b, N;
int n;
double x,inte, frac; //x is output, integer part, fractional part
long double div; //division value of a/b
printf("Enter so nguyen duong a : \n"); scanf("%d",&a);
printf("Enter so nguyen duong b : \n"); scanf("%d",&b);
printf("Enter so nguyen duong N : \n"); scanf("%d",&N);
div = (long double) a / (long double)b;
x = 0;
for(n=0;n<N;n++)
{
frac = modf(div,&inte); //perform separating integer and fractional part of division
x = x + (int)floor(frac*10); //round down the fractional part of division
div=frac*10;
}
printf("a / b = %.20f\n",(double) a / (double)b);
printf ("tong cua %i chu so sau dau phay la = %.0f\n",N,x);
}