Hackety Hacking Problem Longsum Implemented c++


Jump to: navigation, search


Problem Statement

How many cells are in an adult human? Lots. More than anyone could count, and the bigger you are the more cells there would be. Growth is a process of cellular reproduction, so as you grow bigger you are made up of more cells.

Although no exact number can be given, the order of magnitude of the number of cells in a human body can be approximated to 10e14 or one hundred trillion cells.

Thus if you have to perform some arithmetic operation on the number of cells, you have to store it in a data type which should have ideally infinite range of storage(limited by size of Computer!).

So, Can you make a program which take number of cells in human body as input and adds it to another number?

HINT: Do you know one of the limitations of your compiler.Let me introduce you with it. This is that "size of the integers or number with it can work with is limited". So many a times it happens that you cannot do a sum on your compiler because it exceeds the size of integer supported by the compiler. So lets do something so that you can do the calculations without caring about the size of the integer and not even losing precision. Write a program that takes two no and gives output as its sum.Use Strings to achieve the task.

Sample Output

Enter the first no: 5555555555555555555555555
Enter the second no.4444444444444444444444444
The sum of the two numbers is : 9999999999999999999999999

Curriculum Connections

This problem gives student help in understanding few thins mentioned below:

  • How to convert data from one type to another(TYPECASTING)
  • Why typecasting is useful
  • The benefit of using STRINGS
  • Knowing about the limitations of the compiler


#include<iostream>   //Header file

using namespace std; //Namespace a bit of syntax

int main()

//these are two strings used to save the two numbers as there is no bound on the size of the string 

string a;                              
string b;
string c;

//take the two numbers

cout<<"Enter the first no    : ";
reverse(a.begin(),a.end()); //this command reverses the string so that we can eeasily manipulate it
cout<<"\nEnter the second no   : ";
int i;
int maxm=max(a.length(),b.length());
int minm=min(a.length(),b.length());

//this block formats the shorter string to be equal to that of the larger array by appending zeros in the end


//This for loop used to carry out the sum starting from the LSB(Least significant bit)


//This loop is used  to check if there is any carry for the last digit


//This block is used to print the output on the screen 

cout<<"The sum of the two numbers is: ";

//This loop prints the sum in reverse order as we have to show MSB(Most significant Bit) First 


return 0;


Anubhav please see the program I have complicated it a bit as i want to avoid any factor of limit if i use array of char.Please check it.....

Personal tools
  • Log in
  • Login with OpenID
About OLPC
About the laptop
About the tablet
OLPC wiki