Program to calculate simple interest in C
/*calculation of simple interest*/
#include<stdio.h>
/*#include is a preprocessor directive and #include<stdio.h> is a declaration for printf() function*/
int main()
/*main() is a function or a cointainer for a set of statements*/
{
int p,n; /*declaration*/
float r,si; /*declaration*/
p=50000; //p=principal
n=10; //n=number of years
r=2500; //r=rate
si=p*n*r/100; //using formula for simple interest
printf("%f\n",si);
/*printf() displays output on the screen, %f is used to print real value, and \n takes cursor to next line*/
return 0;
/*as we are using int before main(), it return a value 0*/
}
In the above program, Every time we run the program we would get the same output but if you want to supply the values of p, r, and n through the keyboard during the execution, you need to use a function scanf( ).
/*calculation of simple interest, taking values from user*/
#include<stdio.h>
/*#include is a preprocessor directive and #include<stdio.h> is a declaration for printf() function*/
int main()
/*main() is a function or a cointainer for a set of statements*/
{
int p,n; /*declaration*/
float r,si; /*declaration*/
printf("input values of p,n,r");
scanf("%d%d%f",&p,&n,&r);
//&(ampersand) is an address of operator, when we use &p we are telling scanf()at which memory location should it store the value.
si=p*n*r/100; //using formula for simple interest
printf("%f\n",si);
//printf() displays output on the screen, %f is used to print real value, and \n takes cursor to next line.
return 0;
//as we are using int before main(), it return a value 0
}
Comments
Post a Comment
Please do not enter any spam link in the comment box.