Arithmetic Operator is very important for programming. All mathematical operations are done by this operator. This operators are “+”,”–“,”*”,”/”.
With Arithmetic Operators we use Assignment Operator that is “=”. The structure to use this operators is
“Variable = Arithmetic Operations”.
There in Left side we use variable which hold the result value of Right side operations.
In any program we do multiple operations. The compiler follow the mathematical hierarchy that is “*” and “/” operation done first, then “+” and “-“. Sometime this hierarchy depend upon operation.
Now let’s see an example
This equation is
(a+(b*c))/(d-c)
Let’s first see the program
The result for This operation is
With Arithmetic Operators we use Assignment Operator that is “=”. The structure to use this operators is
“Variable = Arithmetic Operations”.
There in Left side we use variable which hold the result value of Right side operations.
In any program we do multiple operations. The compiler follow the mathematical hierarchy that is “*” and “/” operation done first, then “+” and “-“. Sometime this hierarchy depend upon operation.
Now let’s see an example
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter value for variable A\n");
scanf("%d",&a);
printf("Enter value for variable B\n");
scanf("%d",&b);
c=a+b;
printf("Result for Addition %d\n",c);
c=a-b;
printf("Result for Subtraction %d\n",c);
c=a*b;
printf("Result for Multiplication %d\n",c);
c=a/b;
printf("Result for Division %d\n",c);
}
In this example we use all type of arithmetic operations. Now let’s see outputvoid main()
{
int a,b,c;
printf("Enter value for variable A\n");
scanf("%d",&a);
printf("Enter value for variable B\n");
scanf("%d",&b);
c=a+b;
printf("Result for Addition %d\n",c);
c=a-b;
printf("Result for Subtraction %d\n",c);
c=a*b;
printf("Result for Multiplication %d\n",c);
c=a/b;
printf("Result for Division %d\n",c);
}
Enter value for variable A
12
Enter value for variable B
6
Result for Addition 18
Result for Subtraction 6
Result for Multiplication 72
Result for Division 2
We can execute multiple operations in a single line. For that we use brackets “()”. The operation inside “()” are done first. Now let’s find out the output of an equation12
Enter value for variable B
6
Result for Addition 18
Result for Subtraction 6
Result for Multiplication 72
Result for Division 2
This equation is
(a+(b*c))/(d-c)
Let’s first see the program
#include<stdio.h>
void main()
{
int a,b,c,d,e,f;
float result;
a=24;
b=32;
c=10;
d=20;
e=a+(b*c);
f=d-c;
result=(float)e/f;
printf("So the result is %.2f",result);
}
As we can see first we divide the equation in two part and simplify the operation then calculate those part separately. At the end we conclude the final operation. void main()
{
int a,b,c,d,e,f;
float result;
a=24;
b=32;
c=10;
d=20;
e=a+(b*c);
f=d-c;
result=(float)e/f;
printf("So the result is %.2f",result);
}
The result for This operation is
So the result is 34.40

0 comments:
Post a Comment