Table of Contents
In this article, we will discuss the concept of Java Code: 5 Ways to Add Two Numbers (With Examples)
In this post, we are going to learn how to write a program to 5 Ways to Add Two Numbers (With Examples) in Java programming language.
here, I’ll give you separate short programs for each method so it’s easier to understand
In this program, we use a simple arithmetic operator to add two numbers.
Program 1
/*Calculate sum of two numbers in java using + operator */ //Using the plus(+) Operator:
public class addition {
public static void main (String args[]){
int num1 = 50;
//variable declaration and initialization of num 1
int num2 = 30;
//variable declaration and initialization of num 2
int sum = num1 + num2;
//Calculate sum of two numbers in java using + operator
System.out.print("Total is:"+sum);
//Print the result on the screen
}
} When the above code is executed, it produces the following result
Total is : 80
In this program, we use a User defined method to add two numbers.
Program 2
//addition of two numbers using a method
//in java programming
class AddTwoNum{
public static void main(String[] args) {
int num1=56;
int num2=23;
int sum=addNum(num1,num2);
System.out.println("Sum: "+sum);
}
public static int addNum(int a, int b){
return a+b;
}
} When the above code is executed, it produces the following result
Sum: 79
In this program, we use a constructor to add two numbers.
Program 3
//Addition two numbers using constructor
//in Java Programming
public class Cons_Add{
private int result;
public Cons_Add(int a, int b){
this.result=a+b;
}
public int get_Result(){//user defined method
return result;
}
public static void main(String[] args) {
int num_1=75;
int num_2=55;
Cons_Add add=new Cons_Add(num_1,num_2);
System.out.println(" result :"+add.get_Result());
}
} When the above code is executed, it produces the following result
result :130
In this program, we use user-input to add two numbers.
Program 4
//Addition two numbers using user inputr
//using simple additional operator in Java Programming
import java.util.Scanner;
public class NormalAdd{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Input value for num1:");
int num_1=input.nextInt();
System.out.println("Input value for num2:");
int num_2=input.nextInt();
int result=num_1+num_2;
System.out.println("Addition of two numbers using constructor:");
System.out.println(" result :"+result);
}
} When the above code is executed, it produces the following result
Input value for num1: 35 Input value for num2: 45 Addition of two numbers using constructor: result :80
In this program, we use a recursive method to add two numbers.
Program 5
//addion java two num Using recursion
class AddTwoNumUsingRecursion {
//recursive function add two numbers
public static int addTwoNum(int a,int b){
if(b==0){
return a;//Base case:If b become zero, result is a
}
return addTwoNum(a+1, b-1);//increment a, dicrement b
}
public static void main(String[] args) {
int num1=34;
int num2=66;
int sum=addTwoNum(num1,num2);
System.out.println("Sum of " + num1 + " and "+ num2+" : "+ sum);
}
} When the above code is executed, it produces the following result
Sum of 34 and 66 : 100
Now you can see 5 different Java programs to add two numbers
Similar post
Java program to Add two numbers 5 different methods
C language : Add two numbers 5 different methods
C+ language : Add two numbers 5 different methods
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.