Monday, 1 October 2012

STRING ADDITION


#include
#include
int main()
{
    int k1,k2,i,j=0,temp1,temp2,temp=0;
    char str1[10000];
    char str2[10000];
    char stradd[100000];
    scanf("%s",str1);
    scanf("%s",str2);
    k1=strlen(str1);
    k2=strlen(str2);
    k1=k1-1;
    k2=k2-1;
    temp=0;
    for(i=k1;i>=0;i--)
    {
        if(k2>=0){
        temp1=(str1[i]-'0')+(str2[k2]-'0')+temp;
        temp2=temp1%10;
        stradd[j]=temp2+'0';
        j=j+1;
        k2=k2-1;
        temp=temp1/10;
        if(i==0&&temp!=0)
        printf("%d",temp);
        }
        else
        {
            temp1=(str1[i]-'0')+temp;
            temp2=temp1%10;
            stradd[j]=temp2+'0';
            temp=temp1/10;
            if(i==0&&temp!=0)
            printf("%d",temp);
            j=j+1;
        }
    }
    for(i=j-1;i>=0;i--)
    {
        printf("%c",stradd[i]);
    }
    return 0;
}


----***ADDITION BY STRING***----

4 comments:

Kuber Saini said...

Code will fail if second string length is greater than first string. So just need to add few lines to check whether first string's length is large or second. based on length you can start addition.
Otherwise Good Work..:)

Kuber Saini said...

Code will fail if second string length is greater than first string. So just need to add few lines to check whether first string's length is large or second. based on length you can start addition.
Otherwise Good Work..:)

Kuber Saini said...

Code will fail if second string length is greater than first string. So just need to add few lines to check whether first string's length is large or second. based on length you can start addition.
Otherwise Good Work..:)

CHANDAN SINGH said...

yes :)
@kuber saini u r right!!
n thnxx ! i forget to mention that thing !!