coder18s,blogspot.com

Powered by Blogger.

5.Pointers in C Hackerrank Solution

Task

Complete the function void update(int *a,int *b). It receives two integer pointers, int* a and int* b. Set the value of  to their sum, and  to their absolute difference. There is no return value, and no return statement is needed.

a' = a+b

b' = |a-b|


Sample Input

4

5
Sample Output
9
1
          Program Code
#include <stdio.h>

void update(int *a,int *b) {
    int x,y;
    x = *a + *b;
    y = *a - *b;
    *a = x;
    *b =abs(y); 
}

int main() {
    int a, b;
    int *pa = &a, *pb = &b;
    
    scanf("%d %d", &a, &b);
    update(pa, pb);
    printf("%d\n%d", a, b);

    return 0;
}