Question

Write a program to subtract large unsigned integers. Your program should prompt and read in two...

Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in the array. Your program should
then output the difference, there should be no leading zeros. Your program should be able to handle integers with up to 80 digits each. You should not use any concepts from java. In particular please do not use any classes (or struct) or any class methods. The one exception is that you may use cin, cout and the iostream class method.cin.get().
Test data:
123456789 and 123459999

IN C++ PROGRAMMING LANGUAGE PLEASE. THE ANSWER SHOULD NOT HAVE ANY LEADING ZEROES. for eg. 000003210. The answer should come as 3210 only. No extra zeroes on the front !!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

int main()
{
unsigned long int num1,num2;
int arr1[80],arr2[80],arr3[80],r,len1,len2,i=0,j;
//Enter the first integer
cout << "Enter first integer: ";
cin >> num1;
//Enter the second integer
cout << "Enter second integer: ";
cin >> num2;
//loop to get the each digit from first number
while(num1 != 0)
{
// % - reminder of num1 / 10
r = num1%10;
//store the each digit in arr1
arr1[i] = r;
num1 = num1 / 10;
i++;
}
//store the length of arr1
len1 = i;
i=0;
//loop to get the each digit from second number
while(num2 != 0)
{
// % - reminder of num2 / 10
r = num2%10;
//store the each digit in arr2
arr2[i] = r;
num2 = num2 / 10;
i++;
}
//store the length of arr2
len2 = i;
r=0;
cout<< "Output: ";
//Displays the difference between second and first integer
for(i=len2-1,j=0;i>=0;i--,j++)
{
//Subtract the second and first integer
arr3[j] = arr2[i] - arr1[i];
//This logic is used to remove the leading zeros
if(arr3[j] > 0)
{
r = 1;
}
if(r>0)
{
//Print the each digit
cout << arr3[j];
}
}
return 0;
}

main.cpp #include <iostream> 2 using namespace std; 4 int main() unsigned long int num1, num2; int arr1[80], arr2[80], arr3[8// Loop to get the each digit from second number while(num2 != 0) // % - reminder of num2 / 10 r = num2%10; //store the eachEnter first integer: 123456789 Enter second integer: 123459999 Output: 3210

Add a comment
Know the answer?
Add Answer to:
Write a program to subtract large unsigned integers. Your program should prompt and read in two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program to subtract large unsigned integers. Your program should prompt and read in two...

    Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in...

  • a c++ program: For this project you will write a program to add very large (unsigned)...

    a c++ program: For this project you will write a program to add very large (unsigned) integers. The two summands should be stored arrays, one array for each summand. The summands should be stored with one digit per location in the two arrays. Your program should, starting at the least significant digits compute the sum by adding the two integers digit by digit. Do not forget to handle carries. Your program should be able to handle summands with up to...

  • Write a program that asks the user to enter 1000 integers to be stored in an...

    Write a program that asks the user to enter 1000 integers to be stored in an array called "numbers". Since the same integer might occur (exist) in the array multiple times, your program needs to fill a second array, called "Isolate" that contains all the integers from the first array but NOT REPAPTED (every integer will exist only once). Finally, your program will print the integers of the second array sorted by occurrence (occurrence is: the number of times the...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • 3. (Dynamic Array) In C++, the largest int value is 2147483647. So. an integer larger than this cannot be stored and pr...

    3. (Dynamic Array) In C++, the largest int value is 2147483647. So. an integer larger than this cannot be stored and processed as an integer. Similarly, if the sum or product of two positive integers is greater than 2147483647, the result will be incorrect. One way to store and manipulate large integers is to store each individual digit of the number in an array. Your program must, at least, contain a function to read and store a number into an...

  • C programming Strictly - Write a program to sort an array of integers via arrays of...

    C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...

  • Write a Program in C language for: 1. Create a C program to read 7 integers...

    Write a Program in C language for: 1. Create a C program to read 7 integers and store them in an array. Next, the program is to check if the array is symmetric, that is if the first element is equal to the last one, the value of the second one is equal to the value of the last but one, and so on. Since the middle element is not compared with another element, this would not affect the symmetry...

  • 3. Write a program to read a (binary) file of integers, sort the integers, and write...

    3. Write a program to read a (binary) file of integers, sort the integers, and write them back to the same file. Assume that all the numbers can be stored in an array. (Exercise 3) 4. Repeat exercise 3, but assume that only 20 numbers can be stored in memory (in an array) at any one time. Hint: you will need to use at least two additional files for temporary output. Please finish the question in C language programming, thank...

  • Suppose we have two integer arrays with the same type, write an AL program to check...

    Suppose we have two integer arrays with the same type, write an AL program to check whether or not there are two integers, one from each array, with sum equal to zero. If there are such integers exist, print out all such combinations to the console window, otherise, print out "No integers in these two arrays, one from each array, with sum equal to zero." to the console window. December 3. 2018 For example, suppose we have the following two...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT