Reputation: 17
Is there a possible way to sum two different long long int
variables when the result is going to be bigger than a long long int
in C?
Upvotes: 1
Views: 2883
Reputation: 153358
As OP wants to "print the result in the screen", divide the number in 2 parts: Most-Significant-Digits and Least-Significant-Digit.
#include <stdlib.h>
void print_long_long_sum(long long a, long long b) {
if ((a < 0) == (b < 0)) { // a and b of the same sign?
// Sum the Most-Significatn_Digits and Least-Significant Digit separately
int sumLSD = (int) (a % 10 + b % 10);
long long sumMSDs = a / 10 + b / 10 + sumLSD / 10;
sumLSD %= 10;
printf("sum = ");
if (sumMSDs) {
printf("%lld", sumMSDs);
// Since sign already printed, insure next print is positive
sumLSD = abs(sumLSD);
}
printf("%d\n", sumLSD);
} else { // No need to separate as there is no chance for overflow
printf("sum = %lld\n", a + b);
}
}
Upvotes: 1