Addition, Substraction & Multiplication of two 20 digit(Random) numbers using C++.
#include<iostream>
#include<stdlib.h>
#define RESET_COLOR "\e[m"
#define GREEN "\e[32m"
#define A 0
#define B 9
#define N 20
#define SWAP(a,b)\
{ \
a=a+b; \
b=a-b; \
a=a-b; \
}
using namespace std;
class Big_Number_Operation {
public:
int *a; //variable declaration
void random_number_generator(); //Prototype declaration
void display(int *a, int n);
void addition(Big_Number_Operation N1, Big_Number_Operation N2);
void substraction(Big_Number_Operation N1, Big_Number_Operation N2);
void multiplication(Big_Number_Operation N1, Big_Number_Operation N2);
};
void Big_Number_Operation::random_number_generator() {
int i;
a = new int[N];
for (i = 0; i < N; i++)
a[i] = A + rand() % (B - A + 1);
}
void Big_Number_Operation::display(int *a, int n) {
int i;
for (i = 0; i < n; i++)
cout << a[i];
}
void Big_Number_Operation::addition(Big_Number_Operation N1, Big_Number_Operation N2) {
int i, num, carry = 0, *c;
c = new int[N + 1];
for (i = N; i > 0; i--) {
num = N1.a[i - 1] + N2.a[i - 1];
c[i] = (carry + num) % 10;
carry = (carry + num) / 10;
}
c[0] = carry;
cout << "\nAddition: ";
for (i = 0; i <= N; i++)
cout << c[i];
cout << endl;
}
void Big_Number_Operation::substraction(Big_Number_Operation N1, Big_Number_Operation N2) {
int i, carry = 0, *c;
c = new int[N + 1];
cout << "\nSubstraction: ";
if (N1.a[0] < N2.a[0]) {
cout << "-";
for (i = 0; i < N; i++)
SWAP(N1.a[i], N2.a[i]);
}
for (i = N; i > 0; i--) {
if ((N1.a[i - 1] - carry) >= N2.a[i - 1]) {
c[i] = (N1.a[i - 1] - carry) - N2.a[i - 1];
carry = 0;
} else {
c[i] = (N1.a[i - 1] - carry + 10) - N2.a[i - 1];
carry = 1;
}
}
for (i = 1; i <= N; i++)
cout << c[i];
cout << "\n";
}
void Big_Number_Operation::multiplication(Big_Number_Operation N1, Big_Number_Operation N2) {
int i, j, k, sum = 0, num, carry = 0, mul[N][N + 1] = {
{0}}, result[2 * N] = {0}, d = 2 * N;
for (i = N - 1; i >= 0; i--) {
carry = 0;
for (j = N; j > 0; j--) {
num = N2.a[i] * N1.a[j - 1];
mul[N - 1 - i][j] = (carry + num) % 10;
carry = (carry + num) / 10;
}
mul[N - 1 - i][0] = carry;
}
carry = 0;
for (j = N; j > 0; j--) {
sum = 0;
i = 0;
k = j;
while (k <= N && i < N)
sum += mul[i++][k++];
result[--d] = (carry + sum) % 10;
carry = (carry + sum) / 10;
}
cout << "\nMultiplication Matrix:-\n";
for (i = 0; i < N; i++) {
for (k = N; k >= i; k--)
cout << " ";
for (j = 0; j <= N; j++)
cout << mul[i][j];
cout << endl;
}
for (j = N - 1; j >= 0; j--) {
sum = 0;
i = N - 1;
k = j;
while (k >= 0 && i >= 0)
sum += mul[i--][k--];
result[--d] = (carry + sum) % 10;
carry = (carry + sum) / 10;
}
cout << "\nMultiplication: ";
for (i = 0; i < 2 * N; i++)
cout << result[i];
cout << "\n";
}
int main() {
int *a, choice;
int i, j;
Big_Number_Operation obj1, obj2, obj3;
srand(time(NULL));
obj1.random_number_generator();
obj2.random_number_generator();
cout << "Two Numbers each" << N << "digits generated" << endl;
while (1) {
cout << "***** Menu Selection *****\n";
cout << "1: Addition\n2: Substraction\n3: Multiplication\n0: Exit\n";
cout << "Enter Your Choice:";
cin >> choice;
switch (choice) {
case 1:
cout << GREEN << "\n--------------- Addition ---------------\n";
cout << "Num1:";
obj1.display(obj1.a, N);
cout << "\nNum2:";
obj2.display(obj2.a, N);
obj3.addition(obj1, obj2);
cout << "----------------------------------------\n" << RESET_COLOR;
break;
case 2:
cout << GREEN << "\n------------- Substraction -------------\n";
cout << "Num1:";
obj1.display(obj1.a, N);
cout << "\nNum2:";
obj2.display(obj2.a, N);
obj3.substraction(obj1, obj2);
cout << "----------------------------------------\n" << RESET_COLOR;
break;
case 3:
cout << GREEN << "\n----------- Multiplication -------------\n";
cout << "Num1:";
obj1.display(obj1.a, N);
cout << "\nNum2:";
obj2.display(obj2.a, N);
obj3.multiplication(obj1, obj2);
cout << "----------------------------------------\n" << RESET_COLOR;
break;
case 0:
exit(0);
}
}
return 0;
}
Output :
Addition :
--------------- Addition ---------------
Num1:72450150888213090691
Num2:54006517114051584283
Addition: 126456668002264674974
----------------------------------------
#include<stdlib.h>
#define RESET_COLOR "\e[m"
#define GREEN "\e[32m"
#define A 0
#define B 9
#define N 20
#define SWAP(a,b)\
{ \
a=a+b; \
b=a-b; \
a=a-b; \
}
using namespace std;
class Big_Number_Operation {
public:
int *a; //variable declaration
void random_number_generator(); //Prototype declaration
void display(int *a, int n);
void addition(Big_Number_Operation N1, Big_Number_Operation N2);
void substraction(Big_Number_Operation N1, Big_Number_Operation N2);
void multiplication(Big_Number_Operation N1, Big_Number_Operation N2);
};
void Big_Number_Operation::random_number_generator() {
int i;
a = new int[N];
for (i = 0; i < N; i++)
a[i] = A + rand() % (B - A + 1);
}
void Big_Number_Operation::display(int *a, int n) {
int i;
for (i = 0; i < n; i++)
cout << a[i];
}
void Big_Number_Operation::addition(Big_Number_Operation N1, Big_Number_Operation N2) {
int i, num, carry = 0, *c;
c = new int[N + 1];
for (i = N; i > 0; i--) {
num = N1.a[i - 1] + N2.a[i - 1];
c[i] = (carry + num) % 10;
carry = (carry + num) / 10;
}
c[0] = carry;
cout << "\nAddition: ";
for (i = 0; i <= N; i++)
cout << c[i];
cout << endl;
}
void Big_Number_Operation::substraction(Big_Number_Operation N1, Big_Number_Operation N2) {
int i, carry = 0, *c;
c = new int[N + 1];
cout << "\nSubstraction: ";
if (N1.a[0] < N2.a[0]) {
cout << "-";
for (i = 0; i < N; i++)
SWAP(N1.a[i], N2.a[i]);
}
for (i = N; i > 0; i--) {
if ((N1.a[i - 1] - carry) >= N2.a[i - 1]) {
c[i] = (N1.a[i - 1] - carry) - N2.a[i - 1];
carry = 0;
} else {
c[i] = (N1.a[i - 1] - carry + 10) - N2.a[i - 1];
carry = 1;
}
}
for (i = 1; i <= N; i++)
cout << c[i];
cout << "\n";
}
void Big_Number_Operation::multiplication(Big_Number_Operation N1, Big_Number_Operation N2) {
int i, j, k, sum = 0, num, carry = 0, mul[N][N + 1] = {
{0}}, result[2 * N] = {0}, d = 2 * N;
for (i = N - 1; i >= 0; i--) {
carry = 0;
for (j = N; j > 0; j--) {
num = N2.a[i] * N1.a[j - 1];
mul[N - 1 - i][j] = (carry + num) % 10;
carry = (carry + num) / 10;
}
mul[N - 1 - i][0] = carry;
}
carry = 0;
for (j = N; j > 0; j--) {
sum = 0;
i = 0;
k = j;
while (k <= N && i < N)
sum += mul[i++][k++];
result[--d] = (carry + sum) % 10;
carry = (carry + sum) / 10;
}
cout << "\nMultiplication Matrix:-\n";
for (i = 0; i < N; i++) {
for (k = N; k >= i; k--)
cout << " ";
for (j = 0; j <= N; j++)
cout << mul[i][j];
cout << endl;
}
for (j = N - 1; j >= 0; j--) {
sum = 0;
i = N - 1;
k = j;
while (k >= 0 && i >= 0)
sum += mul[i--][k--];
result[--d] = (carry + sum) % 10;
carry = (carry + sum) / 10;
}
cout << "\nMultiplication: ";
for (i = 0; i < 2 * N; i++)
cout << result[i];
cout << "\n";
}
int main() {
int *a, choice;
int i, j;
Big_Number_Operation obj1, obj2, obj3;
srand(time(NULL));
obj1.random_number_generator();
obj2.random_number_generator();
cout << "Two Numbers each" << N << "digits generated" << endl;
while (1) {
cout << "***** Menu Selection *****\n";
cout << "1: Addition\n2: Substraction\n3: Multiplication\n0: Exit\n";
cout << "Enter Your Choice:";
cin >> choice;
switch (choice) {
case 1:
cout << GREEN << "\n--------------- Addition ---------------\n";
cout << "Num1:";
obj1.display(obj1.a, N);
cout << "\nNum2:";
obj2.display(obj2.a, N);
obj3.addition(obj1, obj2);
cout << "----------------------------------------\n" << RESET_COLOR;
break;
case 2:
cout << GREEN << "\n------------- Substraction -------------\n";
cout << "Num1:";
obj1.display(obj1.a, N);
cout << "\nNum2:";
obj2.display(obj2.a, N);
obj3.substraction(obj1, obj2);
cout << "----------------------------------------\n" << RESET_COLOR;
break;
case 3:
cout << GREEN << "\n----------- Multiplication -------------\n";
cout << "Num1:";
obj1.display(obj1.a, N);
cout << "\nNum2:";
obj2.display(obj2.a, N);
obj3.multiplication(obj1, obj2);
cout << "----------------------------------------\n" << RESET_COLOR;
break;
case 0:
exit(0);
}
}
return 0;
}
Output :
Addition :
--------------- Addition ---------------
Num1:72450150888213090691
Num2:54006517114051584283
Addition: 126456668002264674974
----------------------------------------
Substraction :
------------- Substraction -------------
Num1:72450150888213090691
Num2:54006517114051584283
Substraction: 18443633774161506408
----------------------------------------
Multiplication :
------------------------------ Multiplication ----------------------------------
Num1:72450150888213090691
Num2:54006517114051584283
Multiplication Matrix:-
217350452664639272073
579601207105704725528
144900301776426181382
289800603552852362764
579601207105704725528
362250754441065453455
072450150888213090691
362250754441065453455
000000000000000000000
289800603552852362764
072450150888213090691
072450150888213090691
507151056217491634837
072450150888213090691
362250754441065453455
434700905329278544146
000000000000000000000
000000000000000000000
289800603552852362764
362250754441065453455
Multiplication: 3912780313859899872369135871208509209553
-------------------------------------------------------------------------------
******************************** The End ***********************************