I posted a previous thread but it is pretty much dead, so here is a new one.
I'm trying to add to very large numbers digit by digit that the user inputs.
I have it working as long as the numbers are the same length digit wise and the last number does not have a carry.
I'm not sure how to shift the numbers and keep the program working the way its suppose to.
Code:
#include "stdafx.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, char* argv[])
{
unsigned char number1 [256] = {'0'};
unsigned char number2 [256] = {'0'};
unsigned char result [256] = {'0'};
int i, y=0, sum=0, carry=0;
printf ("Please enter a number upto 255 digits long:\n");
scanf ("%s", number1);
printf ("Your first number is %s.\n",number1);
printf ("Please enter another number upto 255 digits long:\n");
scanf ("%s", number2);
printf ("Your second number is %s.",number2);
memset (result, 0, sizeof(result));
for(y=255; y>=0; y--) {
BACK:
if( number1[y] == 0) {
y--;
goto BACK;
}
else if( number2[y] == 0) {
y--;
goto BACK;
}
else{
sum += carry;
sum += number1[y]-'0'+number2[y]-'0';
carry = sum/10;
result[y] = sum%10+'0';
sum = 0;}
}
printf("\nThe sum is %s.", result);
return 0;
}
Thank you for your help.