In this program we will see the addition of two numbers using two different methods. In first program we specify the values of two numbers in the program itself. And in second program take the both numbers entered by user and add. In third program we use add () function.

Adding the numbers using java programming is same logic as add the numbers using other programming languages. Adding the numbers means simply add the values in the variables.

For example:

Numbers to Add: 20 and 50
Let Num1 = 20 and Num2= 50 

Addition: 
Sum= Num1 + Num2 
Sum= 20 + 50

First Program: Add of two numbers

class Add
{
public static void main(String[] args)
{
  int Num1,Num2,Sum;
  Num1=20;
  Num2=50;
  Sum=Num1+Num2;
  System.out.println("Addition of Num1 and Num2:"+Sum);
}
}
 

Output:

Addition of Num1 and Num2: 70

The output of above program is 70 this is the addition of two numbers 20 and 50. but if you can see then you notice that in this program we put the values in the program itself. There are no choice for use to enter the values.

If you want to get the input values from users and use them in program then refer following program. In this program you get the input form users for addition.

Second Program: Addition of two numbers using Scanner

In this addition program we use Scanner class. Scanner is a java class which used for to take input from users.

import java.util.*; 

class Add
{
public static void main(String[] args)
{
  int Num1,Num2,Sum;
  Scanner sc = new Scanner(System.in);  
  System.out.println("Enter the value of Num1:");  
  Num1 = sc.nextInt();  
  System.out.println("Enter the value of Num2:"); 
  Num2 = sc.nextInt();  
  Sum=Num1+Num2;
  System.out.println("Addition of Num1 and Num2:"+Sum);
}
}
 

output:

Enter the value of Num1: 
20 
Enter the value of Num2: 
50 
Addition of Num1 and Num2: 70          
Addition of two numers

Above output is same as the first program’s output but here we take the input from user do not specify the values in the program itself. Next is, below program are same as above program but difference is that we use separate Add() function because it is helpful for reuse.

Third Program: Addition of two numbers using Add() Function

import java.util.*; 

class Addition
{
int Add(int num1, int num2)
{
int sum;
sum=num1+num2;
return sum;
}
public static void main(String[] args)
{
  int Num1,Num2;
  Scanner sc = new Scanner(System.in);  
  System.out.println("Enter the value of Num1:");  
  Num1 = sc.nextInt();  
  System.out.println("Enter the value of Num2:"); 
  Num2 = sc.nextInt();  
  Sum=Add(Num1,Num2);
  System.out.println("Addition of Num1 and Num2:"+Sum);
}
}

Output

Enter the value of Num1: 
20
Enter the value of Num2:
50
Addition of Num1 and Num2: 70

Both the programs produce same output but the ways we used in this programs are different. If you are new in java programming and you don’t know how to run java program then must read How to Run a Java Program.

I hope you enjoy this article & learn something useful from that. If you are having any doubt then feel free to comment me below I will definitely try to respond you!..

Leave a Reply

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

Back to Top