Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In c++. It needs to add, subtract and multipy a number with MORE THAN 20 digits.

ID: 3579159 • Letter: I

Question

In c++. It needs to add, subtract and multipy a number with MORE THAN 20 digits. Will rate! Programming Exercise 11 in Chapter 8 explains how to add large integers using arrays. However, in that exercise, the program could add only integers of, at most, 20 digits. This chapter explains how to work with dynamic integers. Design a class named largelntegers such that an object of this class can store an integer of any number of digits. Add operations to add, subtract, multiply, and compare integers stored in two objects. Also add constructors to properly initialize objects and functions to set, retrieve, and print the values of objects. REFERENCE: (Adding Large Integers) In C++, the largest int value is 2147483647. So, an integer larger than this cannot be stored and processed as an integer. Similarly, if the sum or product of two positive integers is greater than 2147483647, the result will be incorrect. One way to store and manipulate large integers is to store each individual digit of the number in an array. Write a program that inputs two positive integers of, at most, 20 digits and outputs the sum of the numbers. If the sum of the numbers has more than 20 digits, output the sum with an appropriate message. Your program must, at least, contain a function to read and store a number into an array and another function to output the sum of the numbers. (Hint: Read numbers as strings and store the digits of the number in the reverse order.)

Explanation / Answer

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
//Maximum is set to 10000000
#define MAX 100000
using namespace std;
class largeIntegers
{
char a[MAX];
char b[MAX];
public:
//Accept method
void accept()
{
//Accept two numbers
cout<<" Enter the first number : ";
cin>>a;
cout<<" Enter the second number : ";
cin>>b;
}
//Reverse the number
void reverse(char *from, char *to )
{
//Calculates the length
int len = strlen(from);
int l;
//Loops from 0 to length
for(l=0;l<len;l++)
to[l]=from[len-l-1];
//Assigns null character at the end
to[len]='';
}
//Subtraction operation
int Subtraction()
{
char L[MAX], S[MAX];
int l,s,now,hold,diff;
//Calculates the length of the first and second number
l=strlen(a);
s=strlen(b);
//Initially sign is assigned 0
bool sign = 0;
//To store result
char result[MAX];
//If first number is less than the second number swap
if(l<s)
{
strcpy(result,a);
strcpy(a,b);
strcpy(b,result);
now=l; l=s; s=now;
//Assign 1 for negative
sign = 1;
}
//If the first number and second number length is equal
if(l==s)
{
if(strcmp(a, b)<0)
{
strcpy(result,a);
strcpy(a,b);
strcpy(b,result);
now=l; l=s; s=now;
sign =1;
}
}
//Reverse the first number
reverse(a,L);
//Reverse the second number
reverse(b,S);
for(;s<l;s++)
S[s]='0';
S[s]='';
for(now=0,hold=0;now<l;now++)
{
//Find out the difference
diff=L[now]-(S[now]+hold);
//If difference is negative
if(diff<0)
{
hold=1;
result[now]=10+diff+'0';
}
//If difference is positive
else
{
result[now]=diff+'0';
hold=0;
}
}
for(now=l-1;now>0;now--)
{
if(result[now]!='0')
break;
}
//Assigns null character at the end
result[now+1]='';
//Reverse the result
reverse(result,L);
strcpy(result,L);
//Displays the subtraction result
cout<<" Subtraction ";
//If sign contains 1 then display negative sign
if(sign == 1)
cout<<"-";
int len = strlen(result);
for(int i=0;i<len;i++)
cout<<result[i];
printf(" ");
}
//Converts character to integer form of character
void convert()
{
int i;
//Find out the length of both the first and second number
int la=strlen(a)-1;
int lb=strlen(b)-1;
//Converts the first number from character to integer form of character
for(i=0;i<=la;i++)
{
a[i] = a[i] - 48;
}
//Converts the second number from character to integer form of character
for(i=0;i<=lb;i++)
{
b[i] = b[i] - 48;
}
}
//Multiplication Operation
void Multiplication()
{
static char mul[MAX];
char c[MAX];
char temp[MAX];
int i,j,k=0,x=0,y;
long int r=0;
long sum = 0;
int la=strlen(a)-1;
int lb=strlen(b)-1;

for(i=lb;i>=0;i--)
{
r=0;
for(j=la;j>=0;j--)
{
temp[k++] = (b[i]*a[j] + r)%10;
r = (b[i]*a[j]+r)/10;
}
temp[k++] = r;
x++;
for(y = 0;y<x;y++)
{
temp[k++] = 0;
}
}
k=0;
r=0;
for(i=0;i<la+lb+2;i++)
{
sum =0;
y=0;
for(j=1;j<=lb+1;j++)
{
if(i <= la+j)
{
sum = sum + temp[y+i];
}
y += j + la + 1;
}
c[k++] = (sum+r) %10;
r = (sum+r)/10;
}
c[k] = r;
j=0;
for(i=k-1;i>=0;i--)
{
mul[j++]=c[i] + 48;
}
mul[j]='';
cout<<" Multiplication of two numbers : "<<mul;
printf(" ");
}
//Addition operation
void Addition()
{
int cmax, c1, c2, m, i, sum;
int rs[MAX];
c1 = strlen(a);
c2 = strlen(b);
//Reverse of first and second number
strrev(a);
strrev(b);
cmax = c1;
//If first number is less than the second number
if(c1<c2)
{
cmax = c2;
}

m=0;
for(i=0; i< cmax; i++)
{
if(c1==c2 || (i < c1 && i < c2))
{
sum = m + a[i] + b[i];
}
else if(i >= c1)
{
sum = m + b[i];
}
else if(i >= c2)
{
sum = m + a[i];
}
rs[i] = sum % 10;
m = sum/10;
}

if(m)
{
rs[i]=m;
i++;
}
//Displays the addition result
cout<<" Addition of two numbers : ";
for(int j=0; j < i; j++)
{
cout<<rs[i-j-1];
}
printf(" ");
}
};
int main()
{
//Creates 3 object of class large Integers
largeIntegers first, second, third;
//Accept data for Addition
cout<<" Accept data for Addition: ";
first.accept();
//Converts it to integer form of character
first.convert();
//Calls the Addition function
first.Addition();
//Accept data for Subtraction
cout<<" Accept data for Subtraction: ";
second.accept();
//Calls the Subtraction function
second.Subtraction();
//Accept data for Multiplication
cout<<" Accept data for Multiplication: ";
third.accept();
//Converts it to integer form of character
third.convert();
//Calls the multiplication function
third.Multiplication();


return 0;
}

Output :

Accept data for Addition:
Enter the first number : 92573811

Enter the second number : 14385755

Addition of two numbers : 106959566

Accept data for Subtraction:
Enter the first number : 98753118

Enter the second number : 12355112

Subtraction 86398006

Accept data for Multiplication:
Enter the first number : 11532418

Enter the second number : 22111314

Multiplication of two numbers : 0254996915577252

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote