Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Mathematics ProgramRSS Feeds

Program to add two integers of any length

Posted By: Easy Tutor     Category: C++ Programming     Views: 3988

A C++ program to add two integers of any length.

Code for Program to add two integers of any length in C++ Programming

 # 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=newchar[(max_length+5)];
       Number_2=newchar[(max_length+5)];
       Sum=newchar[(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;
    }
  
Share: 


Didn't find what you were looking for? Find more on Program to add two integers of any length Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to add two integers of any length is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!