Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- unsigned addnum(unsigned x[],unsigned y[],unsigned z[])
- {
- unsigned max,i;
- unsigned carry=0;
- if (x[0]>y[0])
- max=x[0];
- else max=y[0];
- for (i=1;i<=max;i++)
- {
- z[i]=(x[i]+y[i]+carry)%10;
- carry=(x[i]+y[i]+carry)/10;
- }
- if (carry!=0)
- {
- z[i]=carry;
- z[0]=max+1;
- }
- else z[0]=max;
- return z[0];
- }
- int main()
- {
- unsigned x[7]={3,7,1,3},y[7]={4,4,2,9,9},z[7]={0};
- printf("%u\n",addnum(x,y,z)); // should be 5 for x[7]={3,7,1,3},y[7]={4,4,2,9,9},z[7]={0}
- for (int i=5;i>=1;i--)
- printf("%u",z[i]); // should be 10241 for x[7]={3,7,1,3},y[7]={4,4,2,9,9},z[7]={0}
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement