Advance Imaginations

It's All About (The) Advance Imaginations.

Sunday, January 1, 2023

Pointer in C HackerRank Solutions



CODE
#include <stdio.h>

void update(int *a,int *b) {
    // Complete this function
    int sum=0, diffmod=0;
    /* calculate sum and diff */
    sum = *a + *b;
    diffmod = *a - *b;
    /* if diff is less then zero multiply -1 to get absolute
       diff or we can use abs() function from standalib     */
    if(diffmod<0)
      diffmod = diffmod*(-1);
    *a = sum;
    *b = diffmod;
}

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


 Thank You

No comments:

Post a Comment

Thanks for spending some time with our blogs; we value your time & feedback. It motivates us to create even more meaningful content further.