Search⌘ K
AI Features

Example 56: Calculate Interest

Explore how to create and apply macro definitions in C for calculating simple interest and total amount. This lesson helps you understand using preprocessor macros to write clean and maintainable financial calculations in your programs.

We'll cover the following...

Problem

Write macro definitions with arguments for the calculation of simple interest and total amount. Store these macro definitions in a file called interest.h.

Include this file in your program, and use the macro definitions for calculating simple interest and amount.

Solution

Given below is our recommended way to approach this problem.

C
#include <stdio.h>
#include "interest.h"
int main() {
int p, n;
float si, amt, r;
p = 1000; n = 3, r = 15.5;
printf ("The Principal, no. of years and rate of interest is %d, %d and %f respectively.\n",p,n,r);
si = SI (p, n, r);
amt = AMT (p, si);
printf ("Simple interest is: %f\n", si);
printf ("Amount is: %f\n", amt);
return 0;
}

Explanation

It is a simple program that calculates simple interest and the total amount after interest. Initially, we define the macro definitions for the following formulae:

  • Simple interest
  • Amount

The formula to calculate simple interest is:

...