Thursday, 12 July 2012

SUM OF LARGE NUMBERS BY USING ARRYS


#include<stdio.h>
#include<conio.h>
#include<string.h>
/* (SUM OF LARGE NUMBERS BY USING ARRYS)*/




void main()
{
char arr1[100], arr2[100];


int f,ans[100]={0,0},x,size1=0,size2=0,value1[100]={0,0},value2[100]={0,0},value3[100]={0,0},cout=0,i=0,sum;


clrscr();


printf("enter your first number");
gets(arr1);


printf("enter your second number");
gets(arr2);
size1=strlen(arr1);

size2=strlen(arr2);

for(x=0;x<size1;x++)
{
value1[x]=arr1[x];
value1[x]=value1[x]-48;
}
for(x=0;x<size2;x++)
{
value2[x]=arr2[x];
value2[x]=value2[x]-48;
}


printf("\n");

for(x=0;x<size1;x++)
{
printf("%d\n",value1[x]);
}
printf("\n\n");

for(x=0;x<size2;x++)
{
printf("%d\n",value2[x]);
}


if(size1>size2)
{
cout=size1;
f=size1-size2;
for(x=0;x<size2;x++)
{
value3[f]=value2[x];
value2[x]=0;
f++;
}
for(x=0;x<size1;x++)
{
value2[x]=value3[x];
}
}


if(size2>size1)
{
cout=size2;
f=size2-size1;
for(x=0;x<size1;x++)
{
value3[f]=value1[x];
value1[x]=0;
f++;
}
for(x=0;x<size2;x++)
{
value1[x]=value3[x];
}
}
if(size1==size2)
{
cout=size1;
}
 clrscr();

printf("\n");

for(x=0;x<cout;x++)
{
printf("%d\t|",value1[x]);

}
printf("\n\n");

for(x=0;x<cout;x++)
{
printf("%d\t|",value2[x]);
}





for (i=cout-1;i>=0;i--)
{
sum=value1[i]+value2[i];
if(sum>=10)
{
ans[i]+=sum%10;
ans[i-1]+=sum/10;
}

if(sum<10)
{
ans[i]=ans[i]+sum;
if(ans[i]>=10)
{
ans[i-1]+=ans[i]/10;
ans[i]=ans[i]%10;
}

}

}



printf("\n\n____________________________________________________  \n\n | ");


for(i=0;i<cout;i++)
{
printf(" %d\t|",ans[i]);
}
getche();

}

No comments:

Post a Comment