Last active
February 4, 2021 04:27
-
-
Save gs-niteesh/d8179209f749152062a1f5489e41b1b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <string.h> | |
| #include <stdlib.h> | |
| void sumBaseB(char *a, | |
| char *b, int base) | |
| { | |
| int len_a, len_b; | |
| len_a = strlen(a); | |
| len_b = strlen(b); | |
| char sum[1000], sa[1000]={0}, sb[1000]={0}; | |
| int diff; | |
| diff = abs(len_a - len_b); | |
| int index = 0; | |
| for (int i = 1; i <= diff; i++) | |
| if(len_a < len_b) | |
| sa[index++] = '0'; | |
| else | |
| sb[index++] = '0'; | |
| strcat(sa, a); | |
| strcat(sb, b); | |
| int curr, carry = 0; | |
| #define max(a, b) (((a) > (b)) ? (a) : (b)) | |
| int mm = 0; | |
| for (int i = max(len_a, len_b) - 1; | |
| i > -1; i--) { | |
| // Current Place value for | |
| // the resultant sum | |
| curr = carry + (sa[i] - '0') + | |
| (sb[i] - '0'); | |
| // Update carry | |
| carry = curr / base; | |
| // Find current digit | |
| curr = curr % base; | |
| // Update sum result | |
| sum[mm++] = (char)(curr + '0'); | |
| } | |
| if (carry > 0) | |
| sum[mm++] = (char)(carry + '0'); | |
| for(int i = mm - 1; i >= 0; i--) { | |
| printf("%c", sum[i]); | |
| } | |
| printf("\n"); | |
| } | |
| int main() { | |
| char s1[1000]; | |
| char s2[1000]; | |
| int n; | |
| scanf("%s %s %d", s1, s2, &n); | |
| sumBaseB(s1, s2, n); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment