Logo 
Search:

C++ Programming Articles

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

Program of addition and subtraction of large numbers

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

This problem is about calculations with large numbers. Numbers can have at most 50 digits. The operations are limited to addition and substraction.

First line contains the number of test cases. Each test case consists of large numbers seperated by operators (plus or minus). Each test case will be terminated by =. All test cases will be seperated by an empty line.

The output contains the result of the evaluated expressions given in the input. Print each test case in a different line.

Code for Program of addition and subtraction of large numbers in C++ Programming

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

 int main( )
    {
       clrscr( );

       fstream File("CP-26.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Data[60]={NULL};
       char Temp[60]={NULL};
       char Operator[60]={NULL};
       char Result[60]={NULL};


       File.getline(Data,50);

       int test_cases=atoi(Data);

       for(int count=1;count<=test_cases;count++)
      {
         strset(Data,NULL);

         File.getline(Data,50);

         strrev(Data);

         do
        {
           strset(Temp,NULL);
           strset(Result,NULL);
           strset(Operator,NULL);

           File.getline(Operator,50);

           if(strcmp(Operator,"=")==0)
              break;

           File.getline(Temp,50);

           strrev(Temp);

           while(strlen(Temp)<strlen(Data))
              strcat(Temp,"0");

           while(strlen(Data)<strlen(Temp))
              strcat(Data,"0");

           int length=strlen(Data);

           if(strcmp(Operator,"+")==0)
              {
             int carry=0;
             int sum=0;
             int digit_1=0;
             int digit_2=0;

             char Sum[5]={NULL};

             for(int i=0;i<length;i++)
                {
                   digit_1=(int(Data[i])-48);
                   digit_2=(int(Temp[i])-48);

                   sum=(digit_1+digit_2+carry);

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

                   else
                  carry=0;

                   itoa(sum,Sum,10);

                   strcat(Result,Sum);
                }

             if(carry)
                strcat(Result,"1");

             strset(Data,NULL);
             strcpy(Data,Result);
              }

           elseif(strcmp(Operator,"-")==0)
              {
             int borrow=0;
             int difference=0;
             int digit_1=0;
             int digit_2=0;

             char Difference[5]={NULL};

             for(int i=0;i<length;i++)
                {
                   digit_1=(int(Data[i])-48);
                   digit_2=(int(Temp[i])-48);

                   difference=(digit_1-digit_2-borrow);

                   if(difference<0)
                  {
                     difference+=10;
                     borrow=1;
                  }

                   else
                  borrow=0;

                   itoa(difference,Difference,10);

                   strcat(Result,Difference);
                }

             strset(Data,NULL);
             strcpy(Data,Result);
              }
        }
         while(1);

         strrev(Data);

         cout<<Data<<endl;

         File.getline(Data,50);
      }

       File.close( );

       getch( );
       return 0;
    }
  
Share: 


Didn't find what you were looking for? Find more on Program of addition and subtraction of large numbers Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program of addition and subtraction of large numbers 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!