I've looked on the site and I've Google'd this for the past couple days to no avail. Either this is just completely obvious and I'm terrible at programming(I'm new, so this is possible).
Anyway, the assignment is to accept two whole numbers that are 512 digits or less. Then we have to subtract the two numbers. Obviously the number is too big for any numerical variable so they have to be stored in another type. I decided string would be the best option, but for some reason when I do my code I get either nothing or a bunch of garbage. I looked at the value of "ans" and the result was garbage. I'm pretty sure the problem is that it's outputting the results of the ASCII value instead of the int that I'm trying to place back into the string. The only libraries I'm allowed to use are the ones I've already included. I'm also supposed to add the two numbers, but if I can figure out how to do the subtraction I could do the addition. I'm not asking someone to write this program for me, just to point me in the right direction. Thanks.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
char str1[512],str2[512],rev1[512],rev2[512],ans1[513],ans2[513];
int num1,num2,num3,num4,len1,len2,len3,len4,len5;
num1 = num2 = num3 = num4 = 0;
printf("Enter a whole number 512 digits or less:");
gets(str1);
printf("\nEnter another whole number 512 digits or less:");
gets(str2);
len1 = strlen(str1);
len2 = 0;
--len1;
while(len2 <= len1)//this loop reverses the number in the first string
{
rev1[len2] = str1[len1 - len2];
++len2;
}
rev1[len2] = '\0';
len2 = 0;
len1 = strlen(str2);
--len1;
while(len2 <= len1) //this loop reverses the number in the second string
{
rev2[len2] = str2[len1 - len2];
++len2;
}
rev2[len2] = '\0';
len3 = strlen(rev1);
len4 = strlen(rev2);
len1 = 0;
len2 = 0;
len5 = 0;
while((len1 <= len3)&&(len2<=len4))
{
num1 = rev1[len1];
num2 = rev2[len2];
num3 = num1 - num2;
if(num3 < 0)//This block is to "borrow" from the next number on the list if the result is less than 0
{
num4 = rev1[len1+1];
num4 = num4 - 1;
rev1[len1+1] = num4;
num3 = num3 + 10;
}
ans1[len5] = num3;
++len5;
++len1;
++len2;
}
len1 = strlen(ans1);
len2 = 0;
--len1;
while(len2 <= len1)//reversing the answer back to original order
{
ans2[len2] = ans1[len1 - len2];
++len2;
}
ans2[len2] = '\0';
printf("%s",ans2);
system("PAUSE");
return 0;
}