I made my program with the use of nested loops with the outer loop counting from 2 to 30 and the inner loop adding up the number of fractions based on the denomination amount. Ex. 1/2 + 1/2 on first iteration, 1/3 + 1/3 + 1/3 on the second iteration, etc.
Here is the code:
int main(void)
{
/* Declare variables */
float sum; /* The sum of the fractions */
int frac_count, /* Counts the number of fractions to be summed in the iteration */
den_count; /* Counts fraction denominations from 2 to 30 */
/* Execute loops to help count denominations and sum up fractions in order to determine representational error */
for (den_count = 2; den_count <= 30; den_count++)
{
sum = 0;
for (frac_count = 1; frac_count <= den_count; frac_count++)
{
/* Add up fractions */
sum += 1 / (float)den_count;
}
/* Compare fractions to 1 and print result */
if (sum == 1)
printf("\nAdding n 1/n's gives a result of 1.");
else if (sum < 1)
printf("\nAdding n 1/n's gives a result less than 1.");
else
printf("\nAdding n 1/n's gives a result greater than 1.");
}
printf("\n");
return 0;
}
The output looks pretty good to me but I want a second opinion just to make sure that the program is executing the way it is supposed to. Thanks. :)


Sign In
Create Account


Back to top









