Hi, I need to write a program where the user inputs 2 numbers and the program will add these two numbers together digit by digit because it has to be able to handle huge numbers. I am storing the input as strings and converting each digit to an integer when adding them.
My problem is that the program is giving me weird results (not even numbers) as the sum of the numbers.
I know my loops are the problem but I'm not sure how to fix them. Please help. Thank you
Here is my code:
Code:
#include "stdafx.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, char* argv[])
{
unsigned char number1 [256];
unsigned char number2 [256];
unsigned char result [256];
int i, y=0, x=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);
for(i=0; i<=256; i++) {
number1 [i];
}
for(y=0; y<=256; y++) {
number2 [y];
}
memset (result, 0, sizeof(result));
for(i=255; i>=0; i--) {
sum += carry;
for(y=255; y>=0; y--) {
sum += number1[i]+number2[y]-48;
}
carry = sum/10;
result[x] = sum%10;
x++;
sum = 0;
}
printf("The sum is %s.", result);
return 0;
}