How to add two numbers Program in java

import java.util.Scanner;
class AddNumbers {
  public static void main(String args[]) {
    int x, y, z;
    System.out.println("Enter two integers to calculate their sum ");
    Scanner in = new Scanner(System.in);
    x = in.nextInt();
    y = in.nextInt();
    z = x + y;
    System.out.println("Sum of entered integers = " + z);
  }
}
//For Large Number
import java.util.Scanner;
import java.math.BigInteger;
class AddingLargeNumbers {
  public static void main(String[] args) {
    String number1, number2;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter first large number");
    number1 = in.nextLine();

    System.out.println("Enter second large number");

    number2 = in.nextLine();
    BigInteger first = new BigInteger(number1);
    BigInteger second = new BigInteger(number2);
    BigInteger sum;
    sum = first.add(second);
    System.out.println("Result of addition = " + sum);
  }
}

Let’s have a look at two programs. One adds two integers, the other uses BigInteger to add large numbers.

Explanation of AddNumbers Step-by-Step

1. Import the Scanner class:

  Import java.util.Scanner;

This statement imports the Scanner class from the package java.util and lets us read input from the user.

2. Class Declaration:

class AddNumbers {

This is a class called AddNumbers. The class contains the code to add two numbers.

3. Declaration of the Main Method:

public static void main(String args[]) {

The primary method is the entry point of the program where execution starts.

4. Declare variables:

  x, y, z;int

  Declare three integer variables: x, y, and z.

  Input two integers in the variables x and y.

  The sum of x and y will be stored in the variable z.

5. Ask for user input:

 System.out.println(“Enter two integers to calculate their sum”);

 Prints out a message to prompt the user to enter two integers.

6. Scanner object to read from console:

  Scanner in = new Scanner(System.in);

  This is a Scanner object called in that reads from the console, reading input from the user.

7. Input for x and y:

  x = in.nextInt();

  y = in.nextInt();

  These lines take two integer numbers from the input and assign them to x and y.

8. Add both numbers:

 z = x + y;

 In this step, it adds both values of x and y in the variable z.

9. Output :

System.out.println(“Sum of entered integers = ” + z)

This statement prints the sum of x and y by printing the value of z.

10. Example of Program Execution

   If the user types in:

   x = 10

   y = 20

Output:

 Input two integers to add them

 10

 20

 Sum of the input integers = 30

Step-by-step Explanation for AddingLargeNumbers:

1. Import the Scanner and BigInteger classes:

  import java.util.Scanner;

  import java.math.BigInteger;

  • The Scanner class is used to read user input.
  • The BigInteger class from java.math is used to handle large integers beyond the range of primitive data types like int and long.

2. Class Declaration:

 class AddingLargeNumbers {

 This defines a class named AddingLargeNumbers, which handles the addition of large numbers.

3. Main Method Declaration:

  public static void main(String[] args) {

  This is where the program starts executing.

4. Declare variables to input a string:

  String number1, number2;

  These two strings will hold the large number entered by the user as strings.

5. Create a Scanner object to scan input:

  Scanner in = new Scanner(System.in);

  A Scanner object is made to read user input from the console.

6. Ask the user to input the first large number

  System.out.println(“Enter first large number”);

  number1 = in.nextLine();

This accepts the first big number from the user and assigns the value to number1.

7. Accept input for the second big number

  System.out.println(“Enter second big number”);

  number2 = in.nextLine();

  This accepts the second big number from the user and assigns the value to number2.

8. convert the input string to BigInteger

BigInteger first = new BigInteger(number1);

BigInteger second = new BigInteger(number2);

The BigInteger constructor is used to convert the input strings (number1 and number2) into BigInteger objects.

9. Add the two BigIntegers:

  BigInteger sum = first.add(second);

  The add() method of BigInteger is used to add the two large numbers and store the result in sum.

10. Print the result:

  System.out.println(“Result of addition = ” + sum);

  This prints the sum of the two large numbers.

11. Program Execution Example:

   If the user enters:

   number1 = “123456789123456789123456789”

   number2 = “987654321987654321987654321”

 Output:

   Enter first large number

   123456789123456789123456789

   Enter second large number

   987654321987654321987654321

   Result of addition = 1111111111111111111111111110

Summary of Output:

For the first program AddNumbers, if the user inputs:

x = 10

y = 20

The output will be:

Enter two integers to calculate their sum

10

20

Sum of entered integers = 30

For the second program AddingLargeNumbers, if the user enters:

number1 = “123456789123456789123456789”

number2 = “987654321987654321987654321”

The output will be:

Enter first large number

123456789123456789123456789

Enter second large number

987654321987654321987654321

Result of addition = 1111111111111111111111111110

Leave a Comment

Your email address will not be published. Required fields are marked *