Lets understand the logic first for the program
and then write it. Its a good practice to think about the logic before
to write the program. So, here we have to perform algebraic operations
like addition, subtraction, multiplication and division. It is obvious
that we need two integers between which you have to perform various
operations. Suppose, say it as x and y. Now, we need four integers here
as the result each for addition, subtraction, multiplication and
division. So, now start with the program:
#include<stdio.h>
void main()
{
int x,y;
int sum, sub, mul, div;
printf("Enter two integer numbers");
scanf("%d",&x,&y);
sum = x+y;
sub = x-y;
mul = x*y;
div = int(x/y);
printf("Sum of the numbers=%d \n",sum);
printf("Subtraction = %d \n",sub);
printf("Multiplication of the numbers = %d \n",mul);
printf("Division between two numbers = %d \n",div);
}
Explanation for the above program
For the explanation of program from starting, you need to go to previous program.
Here, I am explaining the other parts which is not explained earlier.
Lets understand the program through an example. Suppose, two numbers
enterd by user are 5 and 2.
Now, x = 5 and y = 4.
sum = x+y; => sum = 5+4 = 9 .
sub = x-y; => sub = 5-4 = 1.
mul = x*y => mul = 5*4 = 20.
div
= x/y => div = 5/4 = 1.25 But, we have used div = int(x/y). So,
it will take the division result into integer i.e, it will display the
result as div = 1 instead of div = 1.25
This process is called type casting. Remember that type casting always occurs in right side as in above example. You can't use it in left side.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment