Javas type int has a limit on how large an integer it can st

Java\'s type  int has a limit on how large an integer it can store . This limit can be circumvented by representing an integer as an array of digits.

Write a program that prompts the user to enter two big integers , which are stored as strings . The program should use the array  method to add the two numbers and then print the result.

Solution

import java.util.Scanner;

import java.util.BigInteger;

class AddingNumbers

{

public static void main( String[] args)

{

String num1, num2;

Scanner in = new Scanner(System.in);

System.out.println (\" Please enter the first digit\");

num1 = in.nextLine();

System.out.println(\"Please enter the second number\");

num2 = in.nextLine();

BigInteger first = new BigInteger (num1);

BigInteger second = new BigInteger(num2);

BigInteger sum;

sum = first.add(second);

System.out.println(\"The result is \" + sum);

}

}

Java\'s type int has a limit on how large an integer it can store . This limit can be circumvented by representing an integer as an array of digits. Write a pro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site