Add Signed and Unsigned Integers Safely

Code

#include <stdio.h>
#include <stdint.h>

int16_t signed_unsigned_sum(int8_t a, uint8_t b) {
    // Your logic here
    uint16_t sum;
    if(a<0) {
        //printf("%d",a);

        a = a * -1;

       // printf("%d",(uint8_t)a);
        sum = b - (uint8_t)a;
        //printf("%d",sum);
    }
    else sum = a+b;
    return sum;
}

int main() {
    int8_t a;
    uint8_t b;
    scanf("%hhd %hhu", &a, &b);
    printf("%d", signed_unsigned_sum(a, b));
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

-10 20

Expected Output

10