Java Program to Add two Numbers

In this article, you will learn how to write a Java program to add two numbers. Here, you will see multiple solutions for it such as adding or sum of two static numbers, the sum of two dynamic given numbers, and Adding two Numbers using command-line arguments.

Add two Numbers in java

The sum or addition of two numbers in java is very simple. Let’s assume we have two integer numbers x=10 and y=20. Now we will take a sum integer variable to calculate the sum of x and y. Let’s see the code for it below:

public class AddTwoNumbers {

	public static void main(String[] args) {
		
		// declare and initialize two integer numbers
		int x=10;
		int y=20;

		// declare another variable to hold the sum of both numbers
		int sum;

		// calculate the sum
		sum = x + y;

		// print the calculated sum
		System.out.println("The Sum of two Numbers: " + sum);

	}

}

OUTPUT:

The Sum of two Numbers: 30

Add two dynamically given Numbers in Java

Here we will take two integer numbers from the user dynamically (at run-time). To take input at run-time, we can take the Scanner class object for reading the user’s inputs. The Scanner class is used to get user input, and it is found in the “java.util” package. Let’s see the complete code for it below:

import java.util.Scanner;

public class AddTwoNumbers {

	public static void main(String[] args) {

		// declare required variables
		int x, y, sum;

		// create scanner object to take dynamic input
		Scanner sc = new Scanner(System.in);

		System.out.print("Enter first number: ");
		// taking first number
		x = sc.nextInt();

		System.out.print("Enter second number: ");
		// taking second number
		y = sc.nextInt();

		// calculate the sum
		sum = x + y;

		// print the calculated sum
		System.out.println("The sum of the given two Numbers: " + sum);

	}

}

OUTPUT:

Enter first number: 20
Enter second number: 30
The sum of the given two Numbers: 50

Add two Numbers in Java using command-line Arguments

Now we will see, the sum of two numbers that are given from command line arguments. Generally, the inputs that come from the command line arguments are in String datatype. They are stored as strings in the String array passed to the args[] parameter of the main() method in Java. So we will convert the string to an integer using the parseInt() method before storing it in integer datatype. The command line arguments should be passed to the main() method at run-time.

Let’s see the code for it below:

public class AddTwoNumbers {

	public static void main(String[] args) {

		// declare required variables
		int x, y, sum;
        
        // read first number
		x = Integer.parseInt(args[0]);

        // read second number
		y = Integer.parseInt(args[1]);

		// calculate the sum
		sum = x + y;

		// print the calculated sum
		System.out.println("The sum of given two Numbers: " + sum);

	}

}

OUTPUT:

The sum of given two Numbers: 30

In order to execute the above program, we will open the command prompt and compile it first with the command >javac AddTwoNumbers.java then run the command >java AddTwoNumbers 10 20

You can continue reading, Java Program to Subtract two Numbers

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

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