Define large number. check this. c++ project

It's a simple algorithm, but it would take me too long to write out the code (and it's your assignment, so it's not my job to do it for you!).
  • #1
mgc
3
0
For the purposes of this problem we will define a large number as a positive whole number with at least eight digits. For example, 123456789 is a large number. Large numbers must NOT be expressed in exponential form.

Write a program that:

(1) asks for two inputs.

WHAT IS THE FIRST LARGE NUMBER?

WHAT IS THE SECOND LARGE NUMBER?

and

(2) then calculates the product of your two large numbers and prints;

THE PRODUCT OF your first large number
AND your second large number
IS the calculated product.

Test your program with 1234512345123451234512345 as your first large number and 9876598765987659876598765 as your second large number.



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#include<iostream>
#include<iomanip>
using namespace std;

void main()
{
string first, second,
long int answer = 0;
char ans;
bool again = false, ans2 = false;

do {
again = false;
cout<<"Enter the first LARGE number: ";
cin>>first;

cout<<"Enter the LARGE second number: ";
cin>>second;

if (first < 10000000)
{
cout<<"\nOne of the numbers is not 8-digit long. Try again.";
again = true;
cout<<endl;
}


} while (again);

answer = (int) first * (int) second;

cout<<"\n\nThe calculated product of "<<first<<" & "<<second
<<"is "<<setw(16)<<answer;

do{
cout<<"\nDo you want to try again? [Y/N]: ";
cin>>ans;

switch(ans)
{
case 'Y':
case 'y': ans2 = true; break;
case 'N':
case 'n': exit(1); break;
}

/*if
cout<<"Invalid Input. Try again."*/
} while (ans2);
}



can you correct this?
 
Technology news on Phys.org
  • #2
We don't do your homework here. We help you do your own homework.

Hints:
1. Is it valid to compare strings to integers?
2. What is the largest number that can be stored in an int? Can an int hold one of your large numbers? If not, why are you using ints?
 
  • #3
okay. thank you!
 
  • #4
help me to correct this prob. LARGE NUMBER

For the purposes of this problem we will define a large number as a positive whole number with at least eight digits. For example, 123456789 is a large number. Large numbers must NOT be expressed in exponential form.

Write a program that:

(1) asks for two inputs.

WHAT IS THE FIRST LARGE NUMBER?

WHAT IS THE SECOND LARGE NUMBER?

and

(2) then calculates the product of your two large numbers and prints;

THE PRODUCT OF your first large number
AND your second large number
IS the calculated product.

Test your program with 1234512345123451234512345 as your first large number and 9876598765987659876598765 as your second large number.

****************************************************

#include <iostream.h>
#include <stdlib.h>
#include <cmath>

int main ()
{
char mybuffer [3000];
float a;
int b;
cout << "Enter First Large number: ";
cin.getline (mybuffer,30000000);
a = atof (mybuffer);
cout << "Enter Second Large number: ";
cin.getline (mybuffer,3000);
b= atoi (mybuffer);
cout << "Total is: " << a*b;
return 0;
}
 
  • #5
Sadly I know only C, C# and Java, so I can't help you give code for C++.
Still, I think that the given task wants you to use strings (or arrays) for calculation. So, you will put first big number in one string, second one in another and then multiply them together - the same method of multiplication you do - first take one digit and multiply with other number, then second... and in the end sum everything. If even upper thing doesn't work, split strings in smaller pieces and multiply first digit with half of the other number (half meaning half digits), then with the other half. Sum them together... then 2nd digit etc etc.
 
  • #6
You'll need to do some extended precision math. The problem asks you to mulplity two 25 digit numbers with could result in a 50 digit product, which would need a 167 bit number, so might as well use 192 bit numbers, and then do extended precision math, which can be implemented similar to long hand muliplication (there are complicated arbitrary precision math algorithms, but these are complex).
 
  • #7
That's still limited to the size of int
You are probably being asked to use strings to store the digits and then multiply them out in the same way you would do long multiplication by hand.
Storing the resut in another string.
 

Related to Define large number. check this. c++ project

1. What is considered a large number in the context of programming?

A large number in programming is typically any number that exceeds the maximum value that can be stored in a data type. This maximum value varies depending on the programming language and data type being used, but it is usually in the range of millions or billions.

2. How is a large number represented in programming languages?

In most programming languages, a large number is represented using a data type specifically designed for storing large numbers, such as a long or double. These data types have a higher range of values compared to integer data types, allowing them to store larger numbers.

3. Can large numbers be manipulated and used in calculations?

Yes, large numbers can be manipulated and used in calculations just like any other number. However, it is important to use the appropriate data type and ensure that the result does not exceed the maximum value that can be stored.

4. How does a computer handle large numbers in calculations?

Computers handle large numbers in a similar way to how they handle smaller numbers, but with more complex algorithms. For example, when adding or subtracting large numbers, a computer may break them down into smaller chunks and perform the operation on each chunk separately before combining the results.

5. Are there any libraries or functions specifically designed for working with large numbers?

Yes, many programming languages have built-in libraries or functions for working with large numbers. For example, in C++, the boost::multiprecision library provides data types and functions for working with arbitrarily large numbers. It is important to consult the documentation of your chosen programming language for available options.

Similar threads

  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
6
Views
9K
Replies
10
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
5K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top