65 789
Assignments Done
99,4%
Successfully Done
In October 2018

Answer to Question #32324 in Java | JSP | JSF for Abhilash

Question #32324
Q) Write an interactive Java program that adds two integers of up to 50 digits each (Represents integer as an array of digits).
Expert's answer
import java.util.* ;

public class Calculator {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the first integer:");
String a = s.nextLine();
System.out.print("Enter the second integer:");
String b = s.nextLine();
int[] num1 = new int[a.length()];
int[] num2 = new int[b.length()];
for (int i=0;i<a.length();i++) {
num1[i] = (int) a.charAt(i);
num2[i] = (int) b.charAt(i);
}
int[] sum = new int[a.length()];
for (int n=b.length()-1;n>=0;n--)
{
if (num1[n]+num2[n]<10)
{
sum[n]=num1[n]+num2[n];

}
if (num1[n]+num2[n]>=10)
{
sum[n]=(num1[n]+num2[n]);
sum[n-1]=sum[n-1]+1;
}
}
for (int d=0; d<a.length();d++) {
System.out.print(sum[d]+" ");
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
01.07.13, 17:28

<div>
</div><div>import java.util.ArrayList;</div><div>import java.util.Scanner;</div><div>
</div><div>public class Sum {</div><div>
</div><div>& & // Variables</div><div>& & static ArrayList<Integer> result = new ArrayList<>();</div><div>& & static String a, b;</div><div>& & static String min;</div><div>& & static String max;</div><div>& & static Scanner sc = new Scanner(System.in);</div><div>& & static int j = 0;</div><div>& & static int ii;</div><div>
</div><div>& & public static void main(String args[]) {</div><div>& & & & result.add(0);</div><div>
</div><div>& & & & System.out.print("Enter first number: ");</div><div>& & & & a = sc.next();</div><div>& & & & System.out.print("Enter second number: ");</div><div>& & & & b = sc.next();</div><div>& & & & sc.close();</div><div>& & & & if (a.length() >= b.length()) {</div><div>& & & & & & max = a;</div><div>& & & & & & min = b;</div><div>& & & & }</div><div>& & & & if (a.length() < b.length()) {</div><div>& & & & & & max = b;</div><div>& & & & & & min = a;</div><div>& & & & }</div><div>
</div><div>& & & & ii = max.length() - 1;</div><div>& & & & for (int i = min.length() - 1; i >= 0; i--) {</div><div>& & & & & & int temp = Integer.parseInt(min.substring(i, i + 1))</div><div>& & & & & & & & & & + Integer.parseInt(max.substring(ii, ii + 1));</div><div>& & & & & & if (temp < 10) {</div><div>& & & & & & & & result.set(j, result.get(j) + temp);</div><div>& & & & & & & & result.add(0);</div><div>& & & & & & }</div><div>& & & & & & if (temp > 9) {</div><div>& & & & & & & & result.set(j, result.get(j) + temp - 10);</div><div>& & & & & & & & result.add(0);</div><div>& & & & & & & & result.set(j + 1, 1);</div><div>& & & & & & }</div><div>& & & & & & j++;</div><div>& & & & & & ii--;</div><div>& & & & }</div><div>
</div><div>& & & & while (ii >= 0) {</div><div>& & & & & & & & result.set(j, result.get(j) + Integer.parseInt(max.substring(ii, ii + 1)));</div><div>& & & & & & & & result.add(0);</div><div>& & & & & & j++;</div><div>& & & & & & ii--;</div><div>& & & & }</div><div>
</div><div>
</div><div>& & & & if (result.get(result.size() - 1) != 0) {</div><div>& & & & & & System.out.print(result.get(result.size() - 1));</div><div>& & & & }</div><div>& & & & for (int i = result.size() - 2; i >= 0; i--) {</div><div>& & & & & & System.out.print(result.get(i));</div><div>& & & & }</div><div>& & }</div><div>& lt;/div><div>}</div><div>
</div>

hari
25.06.13, 19:53

Q) Write an interactive Java program that adds two integers of up to 50 digits each (Represents integer as an array of digits).

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions