45 101
Assignments Done
98%
Successfully Done
In February 2017
Your physics homework can be a real challenge, and the due date can be really close — feel free to use our assistance and get the desired result.
Be sure that math assignments completed by our experts will be error-free and done according to your instructions specified in the submitted order form.
Our experts will gladly share their knowledge and help you with programming homework. Keep up with the world’s newest programming trends.

Answer on Java | JSP | JSF Question 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]+" ");
}
}
}
tip Your question requires fast and guaranteed response?   Please   SUBMIT IT

Comments

Assignment Expert
01.07.2013 10: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.2013 12: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