Calculate Simple Interest Program

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to write a C program to calculate the Simple Interest for a given principal amount, rate of interest, and time duration.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123332{{"`Calculate Simple Interest Program`"}} c/comments -.-> lab-123332{{"`Calculate Simple Interest Program`"}} c/variables -.-> lab-123332{{"`Calculate Simple Interest Program`"}} c/data_types -.-> lab-123332{{"`Calculate Simple Interest Program`"}} c/operators -.-> lab-123332{{"`Calculate Simple Interest Program`"}} c/user_input -.-> lab-123332{{"`Calculate Simple Interest Program`"}} c/memory_address -.-> lab-123332{{"`Calculate Simple Interest Program`"}} c/pointers -.-> lab-123332{{"`Calculate Simple Interest Program`"}} c/function_declaration -.-> lab-123332{{"`Calculate Simple Interest Program`"}} end

Initialize Variables

Declare and initialize the variables principal_amt, rate, simple_interest, and time as float and integer types respectively, as shown below:

#include <stdio.h>
int main()
{
    float principal_amt, rate, simple_interest;
    int time;
}

Get User Input

Get the principal amount, rate of interest (in percentage), and time duration (in years) from the user using scanf:

printf("Enter the value of principal amount, rate and time\n\n\n");
scanf("%f%f%d", &principal_amt, &rate, &time);

Use %f format specifier to read float type input and %d to read integer type input.

Calculate Simple Interest

Calculate the simple interest using the formula:

Simple Interest = (principal_amt * rate * time) / 100

Use the below code to calculate simple interest:

simple_interest = (principal_amt*rate*time)/100.0;

Display the Output

Print the output text and variables to the console using printf function.

printf("\n\n\t\t\tAmount = Rs.%7.3f\n ", principal_amt);
printf("\n\n\t\t\tRate = Rs.%7.3f\n ", rate);
printf("\n\n\t\t\tTime = %d years \n", time);
printf("\n\n\t\t\tSimple Interest = Rs.%7.3f\n ", simple_interest);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");

Use %7.3f to format the output to 7 digits including 3 digits after a decimal point.

Write the Full Code

Copy the code from Steps 1-4 into the main function in the ~/project/main.c file, as shown below:

#include <stdio.h>

int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    float principal_amt, rate, simple_interest;
    int time;
    printf("Enter the value of principal amount, rate, and time\n\n\n");
    scanf("%f%f%d", &principal_amt, &rate, &time);

    // considering rate is in percentage
    simple_interest = (principal_amt*rate*time)/100.0;

    // usually used to align text in form of columns in table
    printf("\n\n\t\t\tAmount = Rs.%7.3f\n ", principal_amt);
    printf("\n\n\t\t\tRate = Rs.%7.3f\n ", rate);
    printf("\n\n\t\t\tTime= %d years \n", time);
    printf("\n\n\t\t\tSimple Interest = Rs.%7.3f\n ", simple_interest);
    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;
}

Compile and Run

Compile and run the program using a C compiler. The output will prompt for user input of principal amount, rate of interest, and time duration, and then display the calculated Simple Interest.

Summary

In this lab, you learned how to write a C program to calculate Simple Interest using variables, user input, and the simple interest formula. You covered the basic concepts such as declaring and initializing variables, getting user input with scanf, and displaying output using printf. Congratulations on completing the lab!

Other C Tutorials you may like