Define large number. check this. c++ project

  • Context: C/C++ 
  • Thread starter Thread starter mgc
  • Start date Start date
  • Tags Tags
    C++ Project
Click For Summary

Discussion Overview

The discussion revolves around defining and implementing a program in C++ that handles large numbers, specifically focusing on input, multiplication, and output of numbers with at least eight digits. The scope includes programming challenges and technical corrections related to handling large integers in C++.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant defines a large number as a positive whole number with at least eight digits and specifies that they should not be expressed in exponential form.
  • Another participant questions the validity of comparing strings to integers and points out the limitations of the int data type in C++ for storing large numbers.
  • A different participant suggests using strings or arrays for calculations, proposing a method similar to long multiplication for handling large numbers.
  • Some participants mention the need for extended precision math to handle the multiplication of large numbers, indicating that the product could exceed typical data type limits.
  • Concerns are raised about the limitations of using int for large numbers, with suggestions to store digits in strings instead.

Areas of Agreement / Disagreement

Participants express differing views on how to handle large numbers in C++. There is no consensus on a single approach, with multiple suggestions regarding data types and methods for multiplication.

Contextual Notes

Participants highlight limitations related to data types in C++, particularly the inability of standard types to handle very large integers, and suggest alternative methods for multiplication that may involve more complex algorithms.

mgc
Messages
3
Reaction score
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;
count<<"Enter the first LARGE number: ";
cin>>first;

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

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


} while (again);

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

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

do{
count<<"\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
count<<"Invalid Input. Try again."*/
} while (ans2);
}



can you correct this?
 
Technology news on Phys.org
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?
 
okay. thank you!
 
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;
count << "Enter First Large number: ";
cin.getline (mybuffer,30000000);
a = atof (mybuffer);
count << "Enter Second Large number: ";
cin.getline (mybuffer,3000);
b= atoi (mybuffer);
count << "Total is: " << a*b;
return 0;
}
 
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.
 
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).
 
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.
 

Similar threads

  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 29 ·
Replies
29
Views
10K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
10
Views
2K
  • · Replies 14 ·
Replies
14
Views
35K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
7K