Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9300575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:48:44+00:00 2026-06-18T22:48:44+00:00

I’m relatively new to programming :). Suppose I wanted to create a program which

  • 0

I’m relatively new to programming :).

Suppose I wanted to create a program which prompts a user to enter two positive numbers up to 50 digits long, which then subtracts the second number from the first number.

For example:

The user enters the first positive number: 239834095803945862440385983452184985298358

second number: 939542309853120721934217021372984729812

===========================================================================

Program outputs the difference: 238894553494092741718901766430812000568564

OR, if negative: -29837430045

===========================================================================

Each digit of the numbers will be stored as individual elements in an array. Here is how I am currently taking in the user input:

int read_array(int int_array[], int MAX_SIZE) {
    char number;
    int count = 0;
    //set all array entries to 0. 
    for (int i = 0; i < MAX_SIZE; i++){
        int_array[i] = 0;
    }

    do { //processes each individual char in the istream
        cin.get(number);
        // puts char on to the array until it hits the
        // end of the number (end of the line)
        if(( number != '\n') && (count < MAX_SIZE) && (isdigit(number))) {
            int_array[count] = int(number) - int('0');
        }  
        count++; //increments count
    } while (number != '\n');

    //tests if number is too large
    int digitcount = count - 1;
    if (digitcount > MAX_SIZE) {
        cout << endl << "ERROR: The number is above 50 digits!" << endl;
        return 0;
    }

THE PROBLEM:

HOW to do the subtraction is eluding me.I have been trying to solve this problem for two weeks and it’s most likely something trivial I have missed.

I have tried:

  1. Converting array of elements back into one whole int
  2. Writing my own program to do long subtraction on the numbers

etc…

However, the output is ONLY successful up until a certain number of digits and/or if they’re positive/negative numbers. I’m stumped and I’m not sure what the best way is to subtract two positive number arrays to get a successful output that can accommodate for positive and negative numbers as shown in the example. ANY HELP GREATLY APPRECIATED :).

EDIT: my attempts:

#include "long_sub.h"
#include <sstream>
#include <vector>

using namespace std;

int long_sub(int a[], int b[], const int size) {
    stringstream ss;
    int const sizes = 50;
    int c = 0; //borrow number
    int borrow = 1; // the '1' that gets carried to the borrowed number
    int r[sizes];

    for (int i = 0; i < size; i++) {
        r[i] = 0;
    }    
    //initialise answer array to 0.
    for (int i = size - 1; i >= 0; i--) {
        //handles zeros
        if (a[i] < b[i] && a[i]) {
            //takes the borrow from the next unit and appends to a.
            ss << borrow << a[i];
            ss >> c;
            ss.clear(); // clears stringstream for next potential borrow.

            int temp = c - b[i];
            r[i] = abs(temp);
        } else {
            int temp = a[i] - b[i];
            r[i] = abs(temp);
        }
    }

    for (int i = 0; i <= size - 1; i++ ) {
        cout << r[i];
    }
    cout << endl;
    return r[sizes];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:48:45+00:00Added an answer on June 18, 2026 at 10:48 pm

    So the solution to this is pretty much the same as when you do it by hand.

    If we have:

     4321
    -1234
    

    You’d take the last digits in the two numbers, subtract the one below from the one above, 1 - 4 – which of course means you have to borrow from the next digit up, so we remmeber that, and then come up with 7. Now, take the next digit [and remember the “borrow”], and subtract 3 from 2-1 = 8.

    The exact same thing is how you do subtraction on large numbers in computer – do one bit at a time, and if you “borrow”, then you need to take that with you to the next step.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which has BIG numbers and small numbers in it. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Specifically, suppose I start with the string string =hello \'i am \' me And
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I've tracked down a weird MySQL problem to the two different ways I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.