Code for PROGRAM SHOWING ROUND-OFF ERRORS IN FLOATING POINT COMPUTATIONS in C Programming
/*------------------- Sum of n terms of 1/n -------------------*/
main()
{
float sum, n, term ;
int count = 1 ;
sum = 0 ;
printf("Enter value of n\n") ;
scanf("%f", &n) ;
term = 1.0/n ;
while( count <= n )
{
sum = sum + term ;
count++ ;
}
printf("Sum = %f\n", sum) ;
}
Output
Enter value of n
99
Sum = 1.000001
Enter value of n
143
Sum = 0.999999