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.
Get a free quote.
Check the website
for updates.
Receive your completed assignment.
Easy as ABC!
Just provide us with clear instructions and wait for the completed assignment.

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])%10;
        sum[n-1]=sum[n-1]+1;  
      }
    }
    for (int d=0; d<a.length();d++) {
      System.out.print(sum[d]+" ");
    }  
  }
}

Comments

2013-06-25 12:53:37
hari wrote

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

2013-07-01 10:28:42
Expert's answer


import java.util.ArrayList;
import java.util.Scanner;

public class Sum {

    // Variables
    static ArrayList<Integer> result = new ArrayList<>();
    static String a, b;
    static String min;
    static String max;
    static Scanner sc = new Scanner(System.in);
    static int j = 0;
    static int ii;

    public static void main(String args[]) {
        result.add(0);

        System.out.print("Enter first number: ");
        a = sc.next();
        System.out.print("Enter second number: ");
        b = sc.next();
        sc.close();
        if (a.length() >= b.length()) {
            max = a;
            min = b;
        }
        if (a.length() < b.length()) {
            max = b;
            min = a;
        }

        ii = max.length() - 1;
        for (int i = min.length() - 1; i >= 0; i--) {
            int temp = Integer.parseInt(min.substring(i, i + 1))
                    + Integer.parseInt(max.substring(ii, ii + 1));
            if (temp < 10) {
                result.set(j, result.get(j) + temp);
                result.add(0);
            }
            if (temp > 9) {
                result.set(j, result.get(j) + temp - 10);
                result.add(0);
                result.set(j + 1, 1);
            }
            j++;
            ii--;
        }

        while (ii >= 0) {
                result.set(j, result.get(j) + Integer.parseInt(max.substring(ii, ii + 1)));
                result.add(0);
            j++;
            ii--;
        }


        if (result.get(result.size() - 1) != 0) {
            System.out.print(result.get(result.size() - 1));
        }
        for (int i = result.size() - 2; i >= 0; i--) {
            System.out.print(result.get(i));
        }
    }
   
}

Leave a comment

Related Questions

Link to us

Share with friends

Get homework help with Assignment Expert:
free questions
Questions and Answers
approved by clients
I received a perfect score on this. The expert provided more than enough detail for each problem that not only helped my understanding of the concepts in the assignment but got me a solid score on the assignment itself. Can't ask for more than that!
I rate AssignmentExpert.com: 5 out of 5.
Rating: 5
Minh Quan Vo on
solving policy
solving policy
satisfaction
guarantee
paypal