C++/logic for algebraic operation of large number
Expert: Eddie - 10/22/2009
QuestionI have got an end semester project in c++. In this project we have to write the program to add, subtract ,multiply,divide, square root, and square the very very large number. for example 1234987654567891234509876+12343231678676554348999862743874= ?
AnswerHello Nischay, thank you for the question.
In C++, the largest number you can add is UINT_MAX, which has a value of 4 billion and some. If you conform your numbers to that, you would do something like this:
unsigned int first, second, operation;
cout << "Enter the first number: ";
cin >> first;
cout << "Enter the second number: ";
cin >> second;
cout << "1. Add 2. Subtract 3. Multiply 4. Divide";
cin >> operation;
if(operation == 1)
{
cout << "The result is: " << first + second;
}
else if(operation == 2)
{
cout << "The result is: " << first - second;
}
else if(operation == 3)
{
cout << "The result is: " << first * second;
}
else if(operation == 4)
{
cout << "The result is: " << first / second;
}
else
{
cout << "Invalid operation";
}
You would simply wrap that into a while loop until the user hit a certain key to quit.
I hope this information was helpful.
- Eddie