Thursday, January 27, 2011

Output / Error



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) ;
}

6 comments:

  1. 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...

    ReplyDelete
  2. Multiplication of the two numbers is : : 200
    Addition of the two numbers is: : 220

    ReplyDelete
  3. The Multiply Function stores the value of 10*20 into i.
    Resulting into i=200 and j=20
    thus addition cum out to be 220

    ReplyDelete
  4. Plz some one tell me what will be the difference if multiply function would be declared as :
    multiply(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?

    ReplyDelete
  5. 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.
    so ans would have been 200 & 30.

    ReplyDelete
  6. Output: Multiplication of two numbers is: : 200
    Addition 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.

    ReplyDelete