Java program to print or calculate addition of two numbers with sample outputs and example programs. Addition of two numbers program is quite a simple one, we do also write the program in five different ways using standard values, command line arguments, classes and objects, without using addition+ operator, method, BufferedReader with sample outputs and code.
- How to add numbers?
Well, do you really need an explanation for this? I think not.
- Let’s get into the programming part.
Java Program To Print Addition Of Two Numbers
1. Standard Method Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Scanner; class Add { public static void main(String[] arg) { int a,b,c; Scanner sc=new Scanner(System.in); System.out.println("Enter first number"); a=sc.nextInt(); System.out.println("Enter second number"); b=sc.nextInt(); c=addition(a,b); System.out.println(" Addition of two numbers is : "+c); } static int addition(int x,int y) { return x+y; } } |
1 2 3 4 5 |
Enter first number 1 Enter second number 2 Addition of two numbers is : 3 |
2. Using command line arguments
There you go another method using command line arguments : If you have no idea about what are command line arguments in Java. Do check it out complete guide here – what are command line arguments in java with examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Scanner; class Add { public static void main(String[] args) { int a,b,c; Scanner sc=new Scanner(System.in); a=Integer.parseInt(args[0]); System.out.println("number one is : "+a); b=Integer.parseInt(args[1]); System.out.println("number two is : "+b); c=a+b; System.out.println("Addition of two numbers is : "+c); } } |
Output:
1 2 3 4 |
>java Add 50 60 number one is : 50 number two is : 60 Addition of two numbers is : 110 |
3. Using classes and Objects
Print addition of numbers : Another sample program using objects and classes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Scanner; class AdditinCal { int res=0; AdditinCal(int x,int y) { res=x+y; } } class Add { public static void main(String[] arg) { int a,b; Scanner sc=new Scanner(System.in); System.out.println("Enter first number"); a=sc.nextInt(); System.out.println("Enter second number"); b=sc.nextInt(); AdditinCal c=new AdditinCal(a,b); System.out.println("Addition of two numbers is : "+c.res); } } |
Output:
1 2 3 4 5 |
Enter first number 150 Enter second number 150 Addition of two numbers is : 300 |
4. Without Using Addition+ Operator
You can even write the program without using the + operator here is the sample code – Learn more about what are different types of operators in Java with examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; class Add { public static void main(String[] arg) { int a,b,c; Scanner sc=new Scanner(System.in); System.out.println("Enter first number"); a=sc.nextInt(); System.out.println("Enter second number"); b=sc.nextInt(); while(a--!=0) { b++; } System.out.println("Addition of two numbers is : "+b); } } |
Output:
1 2 3 4 5 |
Enter first number 7 Enter second number 5 Addition of two numbers is : 12 |
5. Using Method
Example program #5 : Using method calling to print the addition of two numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class Add { int c; void addition(int x,int y) { c=x+y; } public static void main(String[] arg) { int a,b; Scanner sc=new Scanner(System.in); System.out.println("Enter first number"); a=sc.nextInt(); System.out.println("Enter second number"); b=sc.nextInt(); Add r=new Add(); r.addition(a,b); System.out.println("Addition of two numbers is : "+r.c); } } |
Output:
1 2 3 4 5 |
Enter first number 5 Enter second number 1 Addition of two numbers is : 6 |
6. Using Buffered Reader
To Print addition of numbers using BufferedReader with sample output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.io.*; public class AddBuf { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter first number"); int x = Integer.parseInt(in.readLine()); System.out.println("Enter second number"); int y = Integer.parseInt(in.readLine()); int z=x+y; System.out.println("Addition of two numbers is : "+z); } } |
Output:
1 2 3 4 5 |
Enter first number 15 Enter second number 15 Addition of two numbers is : 30 |
For You:
Here is the list of other programs for you: