C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Menu Driven Loan Calculator Program in C

Introduction

In real-world financial applications, EMI calculators not only compute the monthly EMI but also:

  • Total Payment = EMI × Number of Months
  • Total Interest = Total Payment – Principal

We’ll create a menu-driven program so the user can select what they want to calculate.

 

C Program: Menu Driven Loan Calculator

C

#include <stdio.h>

#include <math.h>   // for pow()

 

int main() {

    float principal, rate, emi, totalPayment, totalInterest;

    int time, n, choice;

    float monthlyRate;

 

    // Input common loan details

    printf("Enter the loan amount (Principal): ");

    scanf("%f", &principal);

 

    printf("Enter annual rate of interest (in %%): ");

    scanf("%f", &rate);

 

    printf("Enter loan tenure (in years): ");

    scanf("%d", &time);

 

    // Convert tenure into months

    n = time * 12;

    // Convert annual interest rate to monthly rate

    monthlyRate = rate / (12 * 100);

 

    // Calculate EMI using formula

    emi = (principal * monthlyRate * pow(1 + monthlyRate, n)) /

          (pow(1 + monthlyRate, n) - 1);

 

    // Calculate total payment and interest

    totalPayment = emi * n;

    totalInterest = totalPayment - principal;

 

    do {

        // Display menu

        printf("\n===== Loan Calculator Menu =====\n");

        printf("1. Calculate EMI\n");

        printf("2. Calculate Total Payment\n");

        printf("3. Calculate Total Interest\n");

        printf("4. Show Full Loan Details\n");

        printf("5. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch(choice) {

            case 1:

                printf("Monthly EMI: %.2f\n", emi);

                break;

            case 2:

                printf("Total Payment (Principal + Interest): %.2f\n", totalPayment);

                break;

            case 3:

                printf("Total Interest Paid: %.2f\n", totalInterest);

                break;

            case 4:

                printf("\nLoan Amount: %.2f", principal);

                printf("\nAnnual Interest Rate: %.2f%%", rate);

                printf("\nTenure: %d years (%d months)", time, n);

                printf("\nMonthly EMI: %.2f", emi);

                printf("\nTotal Payment: %.2f", totalPayment);

                printf("\nTotal Interest: %.2f\n", totalInterest);

                break;

            case 5:

                printf("Exiting program. Goodbye!\n");

                break;

            default:

                printf("Invalid choice! Please enter 1–5.\n");

        }

 

    } while(choice != 5);

 

    return 0;

}

Output

 
OUTPUT :
Enter the loan amount (Principal): 500000
Enter annual rate of interest (in %): 10
Enter loan tenure (in years): 5

===== Loan Calculator Menu =====
1. Calculate EMI
2. Calculate Total Payment
3. Calculate Total Interest
4. Show Full Loan Details
5. Exit
Enter your choice: 4

Loan Amount: 500000.00
Annual Interest Rate: 10.00%
Tenure: 5 years (60 months)
Monthly EMI: 10624.59
Total Payment: 637475.40
Total Interest: 137475.40

Explanation

  1. User enters loan amount, interest rate, and tenure.
  2. EMI is calculated once, using the formula.
  3. Menu options let the user choose:
    • EMI
    • Total Payment
    • Total Interest
    • Full Details
    • Exit
  4. Uses a do-while loop to repeat until exit.