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 9227261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:01:43+00:00 2026-06-18T05:01:43+00:00

For practice, I am trying to add two very long integers by placing them

  • 0

For practice, I am trying to add two very long integers by placing them in arrays and adding the corresponding elements in the arrays. However, when trying to add the carry over, I’m having problems (I.e., the carry over is the 1, that for instance, you add to the tens place when you do 199 + 199 = 398).

When doing 167 + 189 I get the right answer which is 356. However, for this very example though (199 + 199), I’m getting 288 instead of 398. My question is, why do I get an incorrect answer when I do 199 + 199, if the carry over works well when I do 167 + 189?

if (stringNumOneLength == stringNumTwoLength)
    {   int answer; 
        int carryOver = 0;
        int answerArray[] = new int[stringNumOneLength + 1];
        for (int i = 1; i <= stringNumTwoLength; i = i + 1)
        {            
            answer = Character.getNumericValue(stringNumOne.charAt(stringNumOneLength - i)) + Character.getNumericValue(stringNumTwo.charAt(stringNumTwoLength - i) + carryOver);
            System.out.println(answer);
            if (answer >= 10)
            {
                for (int j = 0; j <= 9; j = j + 1)
                {
                    if (10 + j == answer) 
                    {
                        carryOver = 1;
                        answer = j;
                        System.out.println("The carryover is " + carryOver + ".");
                    }
                }
            }
            else
            {
                carryOver = 0;
            }

            answerArray[stringNumOneLength + 1 - i] = answer;
        }
        System.out.println(Arrays.toString(answerArray));
    }

The output is the following:

[1, 9, 9]

[1, 9, 9]

18

The carryover is 1.

8

2

[0, 2, 8, 8]

  • 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-18T05:01:44+00:00Added an answer on June 18, 2026 at 5:01 am

    The parenthesis on this line:

    answer = Character.getNumericValue(stringNumOne.charAt(stringNumOneLength - i)) + Character.getNumericValue(stringNumTwo.charAt(stringNumTwoLength - i) + carryOver);
    

    are wrong. Notice how the + carryOver is within the call to Character.getNumericValue.

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

Sidebar

Related Questions

I have two array with string values, and one dictionary, im trying to add
In my Firefox add-on using the Add-on SDK, I'm trying to include two library
I'm trying to practice writing better code, so I wanted to validate my input
I'm trying to practice my F# by writing small console scripts in F# in
Recently I was trying some practice programs in python and I came across this
Why is my sql injection which i am trying to practice on not working.
I am fairly new to vim. I am trying to practice (been reading a
I'm using Eclipse with C++ plugins on my macbook, trying some practice projects to
I'm trying to learn LINQ by practice. This seems like a situation where I
I am trying to implement a to-do-list website to practice using Django. In models.py,

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.