I was wondering if I could get a second look at my program before I turn it in. The job was to code a program in java that adds two integers to an array gather the sum while displaying the integers that belong in the carry column section. I am curious if there is a simpler way to do what I am trying to do. I hope the output is enough to give you the idea behind the program.
Here is a sample output.
Enter the first number : 9999 Enter the second number : 9999 Carry : 1110 Num1 : 9999 Num2 : 9999 Sum : 19998 Continue(Y/N)
import java.util.Arrays;
import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String args[]) {
Integer[] num1 = new Integer[10];
Integer[] num2 = new Integer[10];
Integer[] temp = new Integer[10];
Integer[] carry = new Integer[10];
Integer[] sum = new Integer[10];
Scanner input = new Scanner(System.in);
do{
int g;
int a=0;
int b=0;
int t;
int s1;
int c=0;
//Read two integer values from user input
System.out.print("\n Enter the first number : ");
int m = input.nextInt();
System.out.print("\n Enter the second number : ");
int n = input.nextInt();
for(int i = 0; i < 10; i++){
num1[i]=0;
num2[i]=0;
}
//Placing the two integer numbers into array
while(m > 0){
g = m % 10;
m = m / 10;
num1[a] = g;
temp[a] = g;
a++;
}
while(n > 0){
g = n % 10;
n = n / 10;
num2[b] = g;
b++;
}
if(a > b) c = a;
else c = b;
for(int i = 0; i < c; i++){
carry[i]=0;
sum[i]=0;
}
//finding sum and handling carry
for(int i = 0; i < c; i++){
sum[i] = num1[i]+num2[i];
s1=sum[i];
t=0;
while(s1 > 0){
s1= s1 / 10;
t++;
}
if(t > 1 && ((i+1)<c)){
carry[i+1] = 1;
num1[i+1] = num1[i+1] + 1;
sum[i] = sum[i] % 10;
}
}
// Printing the sum value to user
System.out.print("\n Carry : " );
for(int x = c-1; x >= 0; x--)
System.out.print(carry[x]);
System.out.print("\n");
System.out.print("\n Num1 : " );
for(int x=c-1;x>=0;x--)
System.out.print(temp[x]);
System.out.print("\n");
System.out.print("\n Num2 : " );
for(int x=c-1;x>=0;x--)
System.out.print(num2[x]);
System.out.print("\n");
System.out.print("\n Sum : " );
for(int x=c-1;x>=0;x--)
System.out.print(sum[x]);
System.out.print("\n");
} while(Continue());
}
public static boolean Continue(){
char choice;
Scanner input = new Scanner(System.in);
System.out.println("Continue(Y/N)");
choice = input.next().charAt(0);
return Character.toLowerCase(choice) == 'y';
}
}