Arjun Suresh (talk | contribs) (Created page with "Complexity <math>\theta(n)</math> No. of swaps in the worst case = <math> \lfloor n/2 \rfloor</math> <syntaxhighlight lang="c" name="largesum"> #include <stdio.h> #include<s...") |
(No difference)
|
Complexity <math>\theta(n)</math>
No. of swaps in the worst case = <math> \lfloor n/2 \rfloor</math>
<syntaxhighlight lang="c" name="largesum">
void print(char *a, int l); int read(char *a);
int main(void) { char a[1000], b[1000], c[1000], carry; int al, bl; printf("Enter the first number "); al = read(a); fflush(stdin); printf("Enter the second number "); bl = read(b); int l = al > bl? al:bl; int i = l; carry = 0; while(al > 0 && bl > 0) { char val = a[--al] + b[--bl] + carry; c[i--] = val %10; carry = val/10; } while(al > 0) { char val = a[--al] + carry;
c[i--] = val %10; carry = val/10;
} while(bl > 0) { char val = b[--bl] + carry;
c[i--] = val %10; carry = val/10;
} c[0] = carry; printf("sum = "); print(c, l+1); return 0; }
int read(char *a) { char c; int i = 0; while(1) { c = getchar(); if(!isdigit(c)) break; a[i++] = c-48; } return i; } void print(char *a, int l) { int i; if(a[0] != 0) printf("%d",a[0]); for(i = 1; i < l; i++) { printf("%d", a[i]); } printf("\n"); }
</syntaxhighlight>
Complexity <math>\theta(n)</math>
No. of swaps in the worst case = <math> \lfloor n/2 \rfloor</math>
<syntaxhighlight lang="c" name="largesum">
void print(char *a, int l); int read(char *a);
int main(void) { char a[1000], b[1000], c[1000], carry; int al, bl; printf("Enter the first number "); al = read(a); fflush(stdin); printf("Enter the second number "); bl = read(b); int l = al > bl? al:bl; int i = l; carry = 0; while(al > 0 && bl > 0) { char val = a[--al] + b[--bl] + carry; c[i--] = val %10; carry = val/10; } while(al > 0) { char val = a[--al] + carry;
c[i--] = val %10; carry = val/10;
} while(bl > 0) { char val = b[--bl] + carry;
c[i--] = val %10; carry = val/10;
} c[0] = carry; printf("sum = "); print(c, l+1); return 0; }
int read(char *a) { char c; int i = 0; while(1) { c = getchar(); if(!isdigit(c)) break; a[i++] = c-48; } return i; } void print(char *a, int l) { int i; if(a[0] != 0) printf("%d",a[0]); for(i = 1; i < l; i++) { printf("%d", a[i]); } printf("\n"); }
</syntaxhighlight>