C Program to Calculate Simple Interest and Amount using Macros

Problem Statement: Write macro definitions with arguments for calculation of Simple Interest and 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.

Related Read:
Macros With Arguments: C Program
C Program to Calculate the Simple Interest

Video Tutorial: C Program to Calculate Simple Interest and Amount using Macros


[youtube https://www.youtube.com/watch?v=uzi8Dkv0ncM]

YouTube Link: https://www.youtube.com/watch?v=uzi8Dkv0ncM [Watch the Video In Full Screen.]

Source Code: C Program to Calculate Simple Interest and Amount using Macros

interest.h

#define SI(p, t, r)  ( (p * t * r) / 100.0 )
#define AMT(p, t, r) ( SI(p, t, r) + p )

main.c

#include<stdio.h>

#include "interest.h"

int main()
{
    float p, t, r;

    printf("Enter principal amount\n");
    scanf("%f", &p);

    printf("Enter Rate of Interest\n");
    scanf("%f", &r);

    printf("Enter Time Period\n");
    scanf("%f", &t);

    printf("Simple Interest: %0.2f\n", SI(p, t, r));
    printf("Total Amount: %0.2f\n", AMT(p, t, r));

    return 0;
}

Output:
Enter principal amount
1000
Enter Rate of Interest
9.2
Enter Time Period
2
Simple Interest: 184.00
Total Amount: 1184.00

In this program we take input for Principal amount, rate of interest and time period from the user, and then calculate Simple Interest for those values and also the total amount accumulated after getting simple interest.

Simple Interest Logic

Simple_Interest = ( Principal_amount * Rate_of_interest * Time ) / 100.0;

Amount = Simple_Interest + Principal_amount;

Note: In Simple Interest formula we are dividing by 100.0 because the ( Principal_amount * Rate_of_interest * Time ) might yield a floating / double type value, so if we divide it by integer 100 then it might give wrong result.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program to Calculate the Simple Interest

In this video tutorial lets learn how we can calculate Simple Interest by making use of simple arithmetic operations.

 
#include<stdio.h>

int main()
{
    int p, t;
    float r, si;

    printf("Enter principal amount\n");
    scanf("%d", &p);

    printf("Enter Rate of interest\n");
    scanf("%f", &r);

    printf("Enter time period\n");
    scanf("%d", &t);

    si = (p * t * r) / 100.0;

    printf("Simple Interest is %f\n", si);

    return 0;
}

Output:
Enter principal amount
1000
Enter Rate of interest
14
Enter time period
2
Simple Interest is 280.000000

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

C Program to Calculate the Simple Interest


[youtube https://www.youtube.com/watch?v=DtgTB1ndPrE]

YouTube Link: https://www.youtube.com/watch?v=DtgTB1ndPrE [Watch the Video In Full Screen.]


In this program we take input for Principal amount, rate of interest and time period from the user, and then calculate Simple Interest for those values.

Simple Interest Logic




We make use of arithmetic operations available in C programming language and convert this formula to calculate Simple Interest.

Simple_Interest = ( Principal_amount * Rate_of_interest * Time) / 100.0;

This gives us Simple Interest and we output the result to the console.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Simple Interest: C Program

Video Tutorial illustrating: C Program To Calculation of Simple Interest.

Header File:

   #include<stdio.h>

To be able to use printf function, we have to include stdio.h header file.

main()
This is the starting point of C program execution.
since it does not return any value, we write void main()

Declaration of Variables:

   int p, n;
float r, si;

variables p and n are integer type variables i.e., they can hold integer values.
variables r and si are float or real type variables i.e., they can hold fractional values.

   clrscr();

to clear the output screen.

Initialization of Variables:

   p = 1000;
n = 3;
r = 8.5;

Principle p = 1000
n or time or duration = 3
rate of interest r = 8.5

Formula to calculate simple interest:

     si = ( p * n * r ) / 100;

The mathematical formula si = ( p x n x r ) / 100; is written as si = ( p * n * r ) / 100;

Output statement:

printf("%f\n",si);

the function printf is present inside stdio.h header file. %f is formatting string for float or real type values.

getch();

function to return to the console window.

Video Tutorial: C Program to calculate Simple Interest


[youtube https://www.youtube.com/watch?v=TGgd6KvL_W0]

YouTube Link: https://www.youtube.com/watch?v=TGgd6KvL_W0 [Watch the Video In Full Screen.]



Full Source code to calculate simple interest in C program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
 
void main()
{
 
int p, n;
float r, si;
 
clrscr();
 
p = 1000;
n = 3;
r = 8.5;
 
si = ( p * n * r ) / 100;
 
printf("%f\n",si);
 
getch();
}

Output:
255.000000