In this HackerRank Pointer problem in c++, we need to develop a program in which The function is declared with a void return type, so there is no value to return. Modify the values in memory so that a contains their sum and b contains their absolute difference.

HackerRank Pointer in C++ problem solution

 

HackerRank Pointer in C++ problem solution

#include <cstdio>
#include <cstdlib>  // required to use the built-in abs() function

void update(int *a,int *b) {
    int temp = *a;
    *a = *a + *b;
    *b = abs(temp - *b);

}

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