Hi there...I can get the input from a user...however I need to add them.

I'm thinking I need a loop to add them, and carry the extra if the person adds them.

Keep in mind, the max size is 20, and I have no added the error checking in there.

import java.util.Scanner;
public class LargeNums {

	
	public static final int MAXSIZE = 20;	
	public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in);
		char x ,y;
		int c1 = MAXSIZE - 1;
		String input1, input2;
		
		int [] num1 = new int[MAXSIZE];
		int [] num2 = new int[MAXSIZE];
		int [] num3 = new int[MAXSIZE];
		
		System.out.println("Please enter two positive integers with less than " + MAXSIZE + " digits" );
		System.out.print("Please enter the first integer: ");
		input1 = keyboard.nextLine();
		System.out.print("Please enter the second integer: ");
		input2 = keyboard.nextLine();



		for (int i= input1.length() -1; i>= 0; i--){

			x = input1.charAt(i);

			if(Character.isDigit(x)){

				num1[c1--] = x-48;
			} 
		}	
		for (int i= input1.length() -1; i>= 0; i--){

			y = input1.charAt(i);

			if(Character.isDigit(y)){

				num2[c1--] = y-48;		
			
			}
		}
		for (int z = 0; z<num1.length; z++){
			System.out.print(num1[z]);
		}
			




	}

}

Recommended Answers

All 3 Replies

So you are just after addition. Do it the way we are taught in primary school. Start by adding the ones, carry, repeat.

You have already defined num3, so this would be a logical place to store the answer, display once you finish processing.

So, loop it, saying start at position 19 in num1, + to position 19 in num2 = num 3.
The issue I forsee is the carry.

Why would you start at position 19? If you start at the start at position 0, you can process the carry as you go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.