C++ Programming Blog

 
 
 

Image

 # include <iostream.h>
 # include   <string.h>
 # include   <stdlib.h>
 # include    <ctype.h>
 # include    <conio.h>


 int main( )
    {
       clrscr( );

       int max_length=1;

       char *Number_1=NULL;
       char *Number_2=NULL;
       char *Sum=NULL;

       int length_1=0;
       int length_2=0;

       cout<<\" Enter the maximum length  = \";
       cin>>max_length;

       Number_1=new char[(max_length+5)];
       Number_2=new char[(max_length+5)];
       Sum=new char[(max_length+5)];

       strset(Sum,NULL);
       strset(Number_1,NULL);
       strset(Number_2,NULL);

       cout<<\"\\n Enter First Number = \";
       cin>>Number_1;

       cout<<\"\\ Enter Second number = \";
       cin>>Number_2;

       length_1=strlen(Number_1);
       length_2=strlen(Number_2);

       if(strlen(Number_1)>=strlen(Number_2))
      {
         strrev(Number_2);

         for(int count_1=length_2;count_1<length_1;count_1++)
        strcat(Number_2,\"0\");

         strrev(Number_2);

         length_2=length_1;
      }

       else
      {
         strrev(Number_1);

         for(int count_2=length_1;count_2<length_2;count_2++)
        strcat(Number_1,\"0\");

         strrev(Number_1);

         length_1=length_2;
      }

       int sum=0;
       int carry=0;
       int digit_1=0;
       int digit_2=0;

       char Temp[5]={NULL};

       for(int count_3=(length_1-1);count_3>=0;count_3--)
      {
         strset(Temp,NULL);

         digit_1=(int(Number_1[count_3])-48);
         digit_2=(int(Number_2[count_3])-48);

         sum=(digit_1+digit_2+carry);

         if(sum>9)
        {
           carry=1;
           sum-=10;
        }

         else
        carry=0;

         itoa(sum,Temp,10);
         strcat(Sum,Temp);
      }

       strrev(Sum);

       cout<<\"\\n Sum = \"<<Sum;

       getch( );
       return 0;
    }

 
 
Didn't find what you were looking for? Find more on Program to add two integers of any length