ComputerShikshak.com

Java - Add Two Numbers

Problem Statement

Write a program in Java to add two numbers entered by the user.

Brief Description

According to the problem statement we have to write a program in Java that will take two numbers as input from the user through the keyboard and will add the two given numbers and will find the sum.

The two numbers given by the user may be integers like 7, 13, 16 etc. or may be floating point or real numbers like 4.7, 8.2, 17.9 etc. or may be the combination of both.

For example, if the user enters 12 and 18 as input, the program that adds two integer numbers will generate 30 as output since 12+18=30.

To view Flowchart click the link given below:

Flowchart – Add Two Numbers

Add Two Integer Numbers Using a Third Variable

In this case two integer numbers given by the user are added and the result is stored in a third variable. Finally the result is displayed along with an appropriate message.

Program

				
					import java.util.*;
class Addition
{
	public static void main(String args[])
	{
		int num1,num2,add;
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter a number: ");
		num1=sc.nextInt();
		System.out.print("Enter another number: ");
		num2=sc.nextInt();
		add=num1+num2;
		System.out.print("Sum is: "+add);
	}
}
				
			

Output

When the program given above is executed we get the following output:

Explanation

In this program:
  • 3 integer variables namely num1, num2 and add are declared.
  • The integer values entered by the user are stored in the variables num1 and num2 respectively.
  • The values stored in num1 and num2 are added and the result is stored in the variable add.

  • Finally the value of add is displayed along with an appropriate message.

Add Two Integer Numbers Without using a Third Variable

In this case two integer numbers given by the user are added and the result is directly displayed (without storing in a third variable) along with an appropriate message.

Program

				
					import java.util.*;
class Addition
{
	public static void main(String args[])
	{
		int num1,num2;
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter a number: ");
		num1=sc.nextInt();
		System.out.print("Enter another number: ");
		num2=sc.nextInt();
		System.out.print("Sum is: "+(num1+num2));
	}
}
				
			

Output

When the program given above is executed we get the following output:

Explanation

In this program:
  • 2 integer variables namely num1 and num2 are declared.
  • The integer values entered by the user are stored in the variables num1 and num2 respectively.
  • Finally the values stored in num1 and num2 are added and the result is directly displayed along with an appropriate message.

Add Two Floating Point or Real Numbers Using a Third Variable

In this case two floating point or real numbers given by the user are added and the result is stored in a third variable. Finally the result is displayed along with an appropriate message.

Program

				
					import java.util.*;
class Addition
{
	public static void main(String args[])
	{
		float num1,num2,add;
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter a number: ");
		num1=sc.nextFloat();
		System.out.print("Enter another number: ");
		num2=sc.nextFloat();
		add=num1+num2;
		System.out.print("Sum is: "+add);
	}
}
				
			

Output

When the program given above is executed we get the following outputs:

Output 1

Output 2

Output 3

Explanation

In this program:
  • 3 float variables namely num1, num2 and add are declared.
  • The numbers entered by the user are stored in the variables num1 and num2 respectively.
  • The values stored in num1 and num2 are added and the result is stored in the variable add.

  • Finally the value of add is displayed along with an appropriate message.
Note:-
  • This program is mainly written to add two floating point or real numbers.
  • Though this program can also be used to add two integer numbers.
  • Except that this program can also be used to add an integer number with a floating point or real number.
  • If the user enters an integer number it will automatically be converted to a floating point or real number while storing the integer number to a float variable.
  • This program can be modified to add two floating point or real numbers without using a third variable.

Add Two Integer Numbers using a Function

In this case two integer numbers given by the user are added using a function and the result is displayed along with an appropriate message.

Program

				
					import java.util.*;
class Addition
{
	static int sum(int n1,int n2)
	{
		int a;
		a=n1+n2;
		return a;
	}
	public static void main(String args[])
	{
		int num1,num2,add;
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter a number: ");
		num1=sc.nextInt();
		System.out.print("Enter another number: ");
		num2=sc.nextInt();
		add=sum(num1,num2);
		System.out.print("Sum is: "+add);
	}
}
				
			

Output

When the program given above is executed we get the following output:

Explanation

In this program:
  • Inside main() method 3 integer variables namely num1, num2 and add are declared.
  • The integer values entered by the user are stored in the variables num1 and num2 respectively inside main() method.
  • The values stored in num1 and num2 are passed to the function sum() which are stored in n1 and n2 respectively.

  • Within sum() function an integer variable namely a is declared.
  • The values stored in n1 and n2 are added and the sum is stored in a.
  • Value of a is returned to the main() function and is stored in the variable add.
  • Finally the value of add is displayed along with an appropriate message.

Add Two Integer Numbers using a Function(Alternate Program-1)

A modified version of the program to “Add Two Integer Numbers using a Function” is as follows:

Program

				
					import java.util.*;
class Addition
{
	int sum(int n1,int n2)
	{
		int a;
		a=n1+n2;
		return a;
	}
	public static void main(String args[])
	{
		int num1,num2,add;
		Scanner sc=new Scanner(System.in);
		Addition ob=new Addition();
		System.out.print("Enter a number: ");
		num1=sc.nextInt();
		System.out.print("Enter another number: ");
		num2=sc.nextInt();
		add=ob.sum(num1,num2);
		System.out.print("Sum is: "+add);
	}
}
				
			

Output

When the program given above is executed we get the following output:

Explanation

In this program:
  • Inside main() method 3 integer variables namely num1, num2 and add are declared.
  • The integer values entered by the user are stored in the variables num1 and num2 respectively inside main() method.
  • The values stored in num1 and num2 are passed to the function sum() which are stored in n1 and n2 respectively.

  • Within sum() function an integer variable namely a is declared.
  • The values stored in n1 and n2 are added and the sum is stored in a.
  • Value of a is returned to the main() function and is stored in the variable add.
  • Finally the value of add is displayed along with an appropriate message.

Add Two Integer Numbers using a Function (Alternate Program-2)

An another modified version of the program to “Add Two Integer Numbers using a Function” is as follows:

Program

				
					import java.util.*;
class Addition
{
	int sum(int n1,int n2)
	{
		return n1+n2;
	}
	public static void main(String args[])
	{
		int num1,num2,add;
		Scanner sc=new Scanner(System.in);
		Addition ob=new Addition();
		System.out.print("Enter a number: ");
		num1=sc.nextInt();
		System.out.print("Enter another number: ");
		num2=sc.nextInt();
		add=ob.sum(num1,num2);
		System.out.print("Sum is: "+add);
	}
}
				
			

Output

When the program given above is executed we get the following output:

Explanation

In this program:
  • Inside main() method 3 integer variables namely num1, num2 and add are declared.
  • The integer values entered by the user are stored in the variables num1 and num2 respectively inside main() method.
  • The values stored in num1 and num2 are passed to the function sum() which are stored in n1 and n2 respectively.

  • Within sum() function the values stored in n1 and n2 are added and the sum is returned to the main() function and is stored in the variable add.
  • Finally the value of add is displayed along with an appropriate message.

Add Two Integer Numbers using a Function (Alternate Program-3)

An another modified version of the program to “Add Two Integer Numbers using a Function” is as follows:

Program

				
					import java.util.*;
class Addition
{
	int sum(int n1,int n2)
	{
		return n1+n2;
	}
	public static void main(String args[])
	{
		int num1,num2;
		Scanner sc=new Scanner(System.in);
		Addition ob=new Addition();
		System.out.print("Enter a number: ");
		num1=sc.nextInt();
		System.out.print("Enter another number: ");
		num2=sc.nextInt();
		System.out.print("Sum is: "+ob.sum(num1,num2));
	}
}
				
			

Output

When the program given above is executed we get the following output:

Explanation

In this program:
  • Inside main() method 2 integer variables namely num1 and num2 are declared.
  • The integer values entered by the user are stored in the variables num1 and num2 respectively inside main() method.
  • The values stored in num1 and num2 are passed to the function sum() which are stored in n1 and n2 respectively.

  • Within sum() function the values stored in n1 and n2 are added and the sum is returned to the main() function and is directly displayed along with an appropriate message.
Share this page on
Scroll to Top