The exercise:

Modify the program in Fig. 4.6 so that it uses only integers to calculate the compound interest. (Hint: Treat all monetary amounts as integral numbers of pennies. Then "break" the result into its dollar portion and cents portion by using the division and modulus operations respectively. Insert a period.)

Fig. 4.6:

Output:Code:/* Fig. 4.6: fig04_06.c Calculating compound interest */ #include <stdio.h> #include <math.h> int main() { int year; double amount, principal = 1000.0, rate = .05; printf( "%4s%21s\n", "Year", "Amount on deposit" ); for ( year = 1; year <= 10; year++ ) { amount = principal * pow( 1.0 + rate, year ); printf( "%4d%21.2f\n", year, amount ); } return 0; }

Code:Year Amount on deposit 1 1050.00 2 1102.50 3 1157.63 4 1215.61 5 1276.28 6 1340.10 7 1407.10 8 1477.46 9 1551.33 10 1628.89

My work:

Output:Code:/* Ex. 4.23: ex4_23.c Modify Fig. 4.6 using integers instead of double variable to calculate compound interest */ #include <stdio.h> int main() { int year, amount = 1, rate = 5, digits, deci, expo; printf( "%4s%21s\n", "Year", "Amount on deposit" ); rate += 100; // 1.05 taken as 105 in integer // calculate compound interest for a duration of 10 years for ( year = 1; year <= 10; year++ ) { // find the power of rate, using expo as exponential value for ( expo = year; expo >= 1; expo-- ) { amount *= rate; //reduce the value of power to a six digit number while ( amount >= 999999 ) { amount /= 10; } } /* while the value of power is less than 6 digits, multiply by 10 till it's a six digits number */ while ( amount <= 100000 ) amount *= 10; digits = amount / 100; // break amount into dollar portion deci = amount % 100; // break amount into cents portion // if the decimal is not a two digit figure, concatenate with a zero if ( deci <= 10) printf( "%4d%18d.0%d\n", year, digits, deci ); else printf( "%4d%18d.%d\n", year, digits, deci ); amount = 1; // reinitialize power to 1 to use again } return 0; }

Code:Year Amount on deposit 1 1050.00 2 1102.50 3 1157.62 4 1215.50 5 1276.27 6 1340.08 7 1407.08 8 1477.43 9 1551.30 10 1628.86

I don't get the exact value as the double variables program. Is the logic that I have used good?