ADDING TWO NUMBERS UP TO 10000 DIGITS
/*We know that by using int ( integer type ) variables we can store it a number up to 9 or 10 digits.Even long int type can store up to 18 or 19 digits.So we can not store or add two numbers greater than this digits.No problem.Here is a simple program that could add two numbers up to 10000 digits.*/
#include <iostream>
using namespace std;
int main()
{
string x,y;
int a[10000],b[10000],C[10000],k = 0;
int counter,counter2;
cin >> x >> y;
if( x.length() >= y.length() ){
for( counter = 0; counter < x.length(); counter++ )
a[counter] = x[counter] - 48;
for( counter2 = 0; counter2 < y.length(); counter2++ )
b[counter2] = y[counter2] - 48;
}
if( x.length() < y.length() ){
for( counter = 0; counter < y.length(); counter++)
a[counter] = y[counter] - 48;
for( counter2 = 0; counter2 < x.length(); counter2++)
b[counter2] = x[counter2] - 48;
}
int rem = 0;
for( int i = counter-1, j = counter2-1; i >=0 || j>=0; i--, j--, k++ )
{
if( i>=0 && j>=0 )
{
int c = a[i]+b[j] + rem;
if(c < 10)
{
rem = 0;
C[k] = c;
}
else
{
c = c-10;
rem = 1;
C[k] = c;
}
}
else
{
int c = a[i]+rem;
if(c<10)
{
rem = 0;
C[k] = c;
}
else
{
c = c-10;
rem = 1;
C[k] = c;
}
}
}
if(rem == 1)
C[k++] = rem;
for(int i = k-1; ;i--)
{
if(C[i] == 0){
k--;
}
else
{
break;
}
}
for(int i = k-1; i >= 0; i--)
cout << C[i];
cout << endl;
return 0;
}
Saturday, 30 July 2016
Subscribe to:
Post Comments (Atom)
Kruskal Algorithm
Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...
-
Introduction: What is Computer Programming?This is the burning question for all who are interested in ...
-
SWAPPING TO NUMBERS WITHOUT TAKING ANY TEMPORARY VARIABLE /*We generally swap or interchange to variables by taking a temporary variabl...
-
Java:Sorting an array //This program will take some random values and sort them in ascending order import static java.lang.Math.abs; i...
No comments:
Post a Comment