The following code is written to get the multiplication and addition
of two given integer numbers. However, the multiplication of the
two numbers is correct but the addition of the two numbers is
incorrect. What is the output ? Identify the error.
#include<stdio .h>
#include<conio.h>
void main()
{
int i=10, j=20;
clrscr();
multiply(&i, &j) ;
addition(i , j) ;
getch() ;
}
multiply(int *1, int *j)
{
*i=*i**j;
printf ("Multiplication of the two numbers is: : %d", *i);
}
addition(int i, int j)
{
i=i+j;
printf("\Addition of the two numbers is: : %d" , i) ;
}
i think k firstly multiply is called n value is stored in i n again i is use for addition thus it displays wrong ans for addition.may be 2020??bt m nt sure...
ReplyDeleteMultiplication of the two numbers is : : 200
ReplyDeleteAddition of the two numbers is: : 220
The Multiply Function stores the value of 10*20 into i.
ReplyDeleteResulting into i=200 and j=20
thus addition cum out to be 220
Plz some one tell me what will be the difference if multiply function would be declared as :
ReplyDeletemultiply(i,j);
and defined as:
multiply(int i, int j)
{
i=i*j;
printf("Multiplication is: : %d", i);
}
would there b any change in the output?
In this i's value would be made 200 but only in the function as soon as it gets out of the function back to the main function, its value would be 10.
ReplyDeleteso ans would have been 200 & 30.
Output: Multiplication of two numbers is: : 200
ReplyDeleteAddition of two numbers is: : 220
Explanation : The user is using pointer in the function
"multiply() ". which changes the value of i from 10 to 200.
Solution : There are two solutions for the problem:
(1) Pass the argument in the multiply function by using pass by
value instead of pass by reference.
(2) Interchange the positions of addition() and multiply()
function.