#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
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.
Today lets learn about basic arithmetic operations in C programming language.
What we learn in this video tutorial?
We shall learn, addition, subtraction, multiplication, division and modular division in this video tutorial. And we’ll also learn how to use pow() method present in math.h library file.
printf("\nSubtraction of a and b is %d\n", (a-b));
printf("\nMultiplication of a and b is %d\n", (a*b));
printf("\nDivision of a and b is %d\n", (a/b));
printf("\nModular Division of a and b is %d\n", (a%b));
printf("\n3 to the power of 2 is %f\n", pow(3,2));
printf("\nASIIC value of A and a is %d and %d\n\n", m, n);
return 0;
}
#include < stdio.h >
#include < math.h >
int main()
{
int a = 10, b = 2;
char m = 'A', n = 'a';
printf("\nAddition of a and b is %d\n", (a+b));
printf("\nSubtraction of a and b is %d\n", (a-b));
printf("\nMultiplication of a and b is %d\n", (a*b));
printf("\nDivision of a and b is %d\n", (a/b));
printf("\nModular Division of a and b is %d\n", (a%b));
printf("\n3 to the power of 2 is %f\n", pow(3,2));
printf("\nASIIC value of A and a is %d and %d\n\n", m, n);
return 0;
}
Output:
Addition of a and b is 12 Subtraction of a and b is 8 Multiplication of a and b is 20 Division of a and b is 5 Modular Division of a and b is 0 3 to the power of 2 is 9.000000 ASIIC value of A and a is 65 and 97
When there is tie between same priority operators then we check the associativity. For example, for * and /, associativity is same. i.e., left to right.
So in the expression: c = a * b + a / b; a * b gets executed first.
ASCII values
When you try to apply Arithmetic Operation on characters(alphabets, character digits, special characters) its correspondent ASCII value gets operated on. ASCII value of A is 65 and ASCII value of a is 97. This is the reason why capital A and small letter a are treated differently in C programming language. Under the hood, they have different ASCII values.
Note 1: All the variables and constants are called operands. All the arithmetic symbols(+, -, /, *, %) are called operators. = is called assignment operator.
Note 2: Only one variable is allowed on the left hand side of the equation or expression. Ex: c = a + b is valid. a + b = c is invalid.
Note 3: You can’t store a decimal value in a integer variable. So if the result of evaluation of an expression has decimal value and you’re assigning it to an integer variable, then only the integer part gets stored. Decimal part will be discarded. So be careful while choosing the data type of variables while performing arithmetic operation.
Note 4: We need to always, explicitly mention the arithematic operators. Ex: c = (a + b)(a – b) throws error. c = (a + b) * (a – b); is the correct way in C programming.
Note 5: An integer value divided by an integer value gives back an integer value. A float value divided by a float value gives back a floating point value.
Note 6: Modular division gives reminder of division. Ex: c = 10 / 2; gives c = 5; c = 10 % 2; give c = 0;
Also, c = -3 % 2; gives c = -1 c = 3 % -2; gives c = 1
Modular division doesn’t take float values as its operands. Ex: c = 3.0 % 2.0; gives error
Note 7: math.h library file has other mathematical operations related methods like: pow(), sin(), cos(), tan(), abs(), sqrt() etc.
We’ll look more arithmetic operators as and when we encounter them in programs.