/********************************************************* * From C PROGRAMMING: A MODERN APPROACH, Second Edition * * By K. N. King * * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * * All rights reserved. * * This program may be freely distributed for class use, * * provided that this copyright notice is retained. * *********************************************************/ /* interest.c (Chapter 8, page 169) */ /* Prints a table of compound interest */ #include /* The following macro is the standard way to determine the size of the array "value" no matter what is the type of elements in this array. Notice this macro can be defined before the array value[5] is declared. */ #define NUM_RATES ((int) (sizeof(value) / sizeof(value[0]))) #define INITIAL_BALANCE 100.00 int main(void) { int i, low_rate, num_years, year; double value[5]; printf("Enter interest rate: "); scanf("%d", &low_rate); printf("Enter number of years: "); scanf("%d", &num_years); printf("\nYears"); for (i = 0; i < NUM_RATES; i++) { printf("%6d%%", low_rate + i); value[i] = INITIAL_BALANCE; } printf("\n"); for (year = 1; year <= num_years; year++) { printf("%3d ", year); for (i = 0; i < NUM_RATES; i++) { value[i] += (low_rate + i) / 100.0 * value[i]; printf("%7.2f", value[i]); } printf("\n"); } return 0; } /* Enter interest rate: 2 Enter number of years: 9 Years 2% 3% 4% 5% 6% 1 102.00 103.00 104.00 105.00 106.00 2 104.04 106.09 108.16 110.25 112.36 3 106.12 109.27 112.49 115.76 119.10 4 108.24 112.55 116.99 121.55 126.25 5 110.41 115.93 121.67 127.63 133.82 6 112.62 119.41 126.53 134.01 141.85 7 114.87 122.99 131.59 140.71 150.36 8 117.17 126.68 136.86 147.75 159.38 9 119.51 130.48 142.33 155.13 168.95 */