C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Graphically Compare of Simple Interest vs Compound Interest using ASCII bars Program in C

Introduction (Graphical SI vs CI)

  • Simple Interest (SI) grows in a straight line (linear growth).
  • Compound Interest (CI) grows faster each year (exponential growth).

This program will print ASCII bar charts year-wise, showing SI and CI side by side.

 

C Program: Graphical SI vs CI

C

#include <stdio.h>

#include <math.h>

 

// Function to print bar of given length

void printBar(float value) {

    int length = (int)(value / 10); // scale down for console

    for (int i = 0; i < length; i++) {

        printf("#");

    }

}

 

int main() {

    float principal, rate, si, ci, amount;

    int years, i;

 

    // Input values

    printf("Enter Principal amount: ");

    scanf("%f", &principal);

 

    printf("Enter Rate of Interest (in %%): ");

    scanf("%f", &rate);

 

    printf("Enter Time (in years): ");

    scanf("%d", &years);

 

    // Print header

    printf("\nYear | Simple Interest (Bar)     | Compound Interest (Bar)\n");

    printf("-------------------------------------------------------------\n");

 

    for (i = 1; i <= years; i++) {

        // Calculate SI

        si = (principal * rate * i) / 100;

 

        // Calculate CI

        amount = principal * pow((1 + rate / 100), i);

        ci = amount - principal;

 

        // Print year and bars

        printf("%4d | ", i);

        printBar(si);

        printf(" (%.2f)", si);

 

        printf("\t| ");

        printBar(ci);

        printf(" (%.2f)\n", ci);

    }

 

    return 0;

}

Output

 
OUTPUT :
Enter Principal amount: 1000
Enter Rate of Interest (in %): 10
Enter Time (in years): 5

Year | Simple Interest (Bar)     | Compound Interest (Bar)
-------------------------------------------------------------
   1 | ########## (100.00)       | ########## (100.00)
   2 | #################### (200.00)   | ##################### (210.00)
   3 | ############################## (300.00)   | ################################### (331.00)
   4 | ######################################## (400.00)   | ################################################# (464.10)
   5 | ################################################## (500.00)   | ###################################################################### (610.51)

Explanation

  1. A helper function printBar() prints # characters scaled to interest values.
    • Scaling ensures the chart fits on the screen.
  2. For each year (loop):
    • Compute SI and CI.
    • Print bars (# symbols) proportional to values.
    • Show actual values beside the bars.
  3. The table clearly shows linear SI vs exponential CI growth.