I coded this in order to sum two maximum 50 digits number (eg: 100000000000000000... + 10000000000000000... )
So I made an array like this " A[50]" and B[50]" then a "for" loop and then it sums A[1] with A[2]....
But the first "for" loop is wrong (the first loop sets all a[i] and b[i]s zero) but I really don't know why it's wrong?! please help me! here's the code: (Assuming that all of the digits are maximum 4)
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int a[50];
int b[50];
int i=0;
int j=0;
int K=0;
for (int v=0; v<=50; v=v+1) \\has infinite loop error!!!!!
{
a[v]=0;
b[v]=0;
}
for (;;)
{
cout<<"Please insert a number devided in 10th two times, -123 to finish";
cin>>K;
if (K==-123) break;
cin>>a[i];
i+=1;
}
cout<<"Please type + to sum otherwise - before your 2nd number \n";
while (2==2)
{
cout<<"Please insert a number devided in 10th two times, -123 to finish";
cin>>K;
if (K==-123) break;
cin>>b[j];
j+=1;
}
i=0; j=0; K=0;
while (i<=49)
{
K=a[i]+b[j]+K;
i=i+1;
j=j+1;
}
cout<<" \n"<<K;
getch();
}
Thanks for your tricky answer! but a new problem happened:
When I want to sum 14+21 it says 8! because it sums 1+4+2+1!!! I can't solve it, any ideas how to solve that?
You could make this significantly more straight-forward if you used strings. I would agree if someone said that using strings to manipulate numbers is counter-intuitive and kinda hacky, but it does lend itself useful to extract individual digits.
Are you familiar with strings?
#include<conio.h>
#include<iostream.h>
#include<math.h>
void main()
{
clrscr();
int a[50];
int b[50];
int i=0;
int j=0;
int MTV=0;
for (int v=0; v<50; v=v+1)
{
a[v]=0;
b[v]=0;
}
for (;;)
{
cout<<"Please insert a number devided in 10th two times, -123 to finish";
cin>>MTV;
if (MTV==-123) break;
cin>>a[i];
i+=1;
}
cout<<"Please your 2nd number \n";
char key;
cout<<"Please say j if it's jam or k if it's kam :D";
key=getch();
if (key=='j') {
while (2==2)
{
cout<<"Please insert a number devided in 10th two times, -123 to finish";
cin>>MTV;
if (MTV==-123) break;
cin>>b[j];
j+=1;
}
}
elseif (key=='k')
{
while (2==2)
{
cout<<"Please insert a number devided in 10th two times, -123 to finish";
cin>>MTV;
if (MTV==-123) break;
cin>>b[j];
b[j]=b[j]*(-1);
j+=1;
}
}
i=0; j=0; MTV=0; int c[50]; int vedadi=0;
while (i<=49)
{
c[i]=a[i]+b[i]+vedadi;
vedadi=0;
if (c[i]>10)
{
vedadi=c[i]/10;
c[i]=c[i]%10;
}
MTV=(c[i]*pow(10,i))+MTV;
i=i+1;
j=j+1;
}
cout<<" \n"<<MTV;
getch();
}