- Java Basic Programs
- Java Programming Examples
- Java Print Hello World
- Java Get Input from User
- Java Print Integer
- Java Add two Numbers
- Java Check Even or Odd
- Java Check Prime or Not
- Java Check Alphabet or Not
- Java Check Vowel or Not
- Check Reverse equal Original
- Java Fahrenheit to Celsius
- Java Celsius to Fahrenheit
- Java Perfect Number Program
- Java Find Quotient Remainder
- Java Days to Seconds
- Java Count Digits in Number
- Java Binary Number Addition
- Java Discount Program
- Java Compute Courier Charge
- Java Find Telephone Bill
- Java Print ASCII Values
- Java Check Palindrome or Not
- Java Check Armstrong or Not
- Generate Armstrong Numbers
- Add two Numbers using Pointers
- Java Mathematical Programs
- Add Subtract Multiply & Divide
- Java Make Calculator
- Java Add Digits of Number
- Java Check Leap Year or Not
- Java Check Divisibility
- Java Find Simple Interest
- Java Find Compound Interest
- Java Print Fibonacci Series
- Java Find nCr nPr
- Calculate Average & Percentage
- Java Calculate Arithmetic Mean
- Java Calculate Student Grade
- Java Print Table of Number
- Java Print Prime Numbers
- Java Add n Numbers
- Java Interchange two Numbers
- Java Reverse Numbers
- Java Swap two Numbers
- Count Positive Negative & Zero
- Find Largest of two Numbers
- Find Largest of three Numbers
- Java Find Factorial of Number
- Java Find HCF & LCM
- Area & Perimeter of Square
- Area & Perimeter of Rectangle
- Area & Circumference of Circle
- Java Conversion Programs
- Java Decimal to Binary
- Java Decimal to Octal
- Java Decimal to Hexadecimal
- Java Binary to Decimal
- Java Binary to Octal
- Java Binary to Hexadecimal
- Java Octal to Decimal
- Java Octal to Binary
- Java Octal to Hexadecimal
- Java Hexadecimal to Decimal
- Java Hexadecimal to Binary
- Java Hexadecimal to Octal
- Java Pattern Programs
- Java Pattern of Stars
- Java Pattern of Alphabets
- Java Pattern of Numbers
- Java Pyramid of Stars
- Java Pyramid of Alphabets
- Java Pyramid of Numbers
- Java Print Diamond Pattern
- Java Print Floyd Triangle
- Java Print Pascal Triangle
- Java Array Programs
- One Dimensional Array Program
- Java Linear Search
- Java Binary Search
- Find Largest Element in Array
- Find Smallest Element in Array
- Java Reverse Array
- Insert Element in Array
- Delete Element from Array
- Java Merge two Array
- Java Bubble Sort
- Java Selection Sort
- Java Insertion Sort
- Java Find Common Elements
- Java Count Even/Odd Number
- Two Dimensional Array Program
- Java Add two Matrices
- Java Subtract two Matrices
- Java Transpose Matrix
- Multiply two Matrices
- Three Dimension Array Program
- Java String Programs
- Java Print String
- Find Length of String
- Java Compare two String
- Java Copy String
- Java Concatenate String
- Java Reverse String
- Delete Vowels from String
- Delete Words from Sentence
- Find Occurrence of a Character
- Java Find Occurrence of a Word
- Occurrence of Each Character
- Java Occurrence of Each Word
- Java Count Repeated Characters
- Java Count Repeated Words
- Java Capitalize Each Word
- Java Count Vowels/Consonants
- Java Extract Numbers
- Java Count Word in String
- Remove Spaces from String
- Java Sort a String
- Java Uppercase to Lowercase
- Java Lowercase to Uppercase
- Java Swap two Strings
- Java Check Anagram or Not
- Java Check Balance Parentheses
- Java Check Password Strength
- Java File Programs
- Java Read File
- Java Write to File
- Read & Display File Content
- Java Copy File
- Java Append Text to File
- Java Merge two File
- List files in Directory
- Java Delete File
- Java Miscellaneous Programs
- Generate Random Numbers
- Java Print Time & Date
- Java Get IP Address
- Java Shutdown Computer
- Java Programming Tutorial
- Java Tutorial
Java Program to Add Two Numbers
This article is created to cover one of the most popular program in Java. That program is to add two numbers. I've created the same program using multiple ways in Java. Here are the list of ways covered in this article:
- Simplest program of adding two numbers in Java
- Add two numbers using Scanner (or by user-input)
- Add two numbers using user-defined function
- Add two numbers using class
- Add two numbers using constructor
Add Two Numbers in Java - Simplest Version
The question is, write a Java program to add two numbers. The program given below is the answer to this question:
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int numberOne = 5, numberTwo = 10, add; add = numberOne + numberTwo; System.out.println("Result = " +add); } }
The output produced by above Java program is:
Result = 15
Now let's move on, and create a program in Java that takes inputs from user and then adds that given two numbers.
Add Two Numbers in Java using Scanner
The question is, write a Java program to add any two numbers. Both the number must be received by user at run-time of the program. The program given below is its answer:
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int numberOne, numberTwo, add; Scanner s = new Scanner(System.in); System.out.print("Enter the First Number: "); numberOne = s.nextInt(); System.out.print("Enter the Second Number: "); numberTwo = s.nextInt(); add = numberOne + numberTwo; System.out.println("\nResult = " +add); } }
The snapshot given below shows the sample run of above program, with user input 100 and 250 as two numbers:
The above program can also be created in this way:
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter Two Numbers: "); int a = s.nextInt(); int b = s.nextInt(); System.out.println("\nResult = " +(a+b)); } }
The snapshot given below shows the sample run of above Java program with user input 13 and 23 as two numbers:
There is a limitation with above program. That limitation is, the program only works with integer. Therefore let me create another program that works for both integer and real numbers:
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter Two Numbers: "); float a = s.nextFloat(); float b = s.nextFloat(); System.out.println("\nResult = " +(a+b)); } }
Here is its sample run with user input 10.23 and 40.32 as two numbers:
Add Two Numbers in Java using Function
This program is created to show, how the addition of two numbers can be performed in Java using a user-defined function. That is, a function named add() is created, that takes two arguments and returns the addition result of its arguments.
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter the First Number: "); float a = s.nextFloat(); System.out.print("Enter the Second Number: "); float b = s.nextFloat(); System.out.println("\nResult = " +add(a, b)); } public static float add(float x, float y) { return (x+y); } }
The sample run of above program is:
Add Two Numbers in Java using Class
Here is another program created using a class. That is, an object obj of the class fresherearth is created. And using the object obj, we can call the method add() of the class. Rest of the things are similar to previous program, that was created using function.
import java.util.Scanner; public class fresherearth { static int add(int x, int y) { return (x+y); } public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter the First Number: "); int a = s.nextInt(); System.out.print("Enter the Second Number: "); int b = s.nextInt(); fresherearth obj = new fresherearth(); int res = obj.add(a, b); System.out.println("\nResult = " +res); } }
Add Two Numbers in Java using Constructor
This is the last program of this article, created using a constructor. A constructor is basically a method having its name, same as the name of the class. Constructor gets automatically executed when an object of the class will get created.
import java.util.Scanner; public class fresherearth { int add; fresherearth(int x, int y) { add = x + y; System.out.println("\nResult = " +add); } public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter the First Number: "); int a = s.nextInt(); System.out.print("Enter the Second Number: "); int b = s.nextInt(); fresherearth obj = new fresherearth(a, b); } }
The above program produces similar result/output as of previous program. In above program, after executing the statement:
fresherearth obj = new fresherearth(a, b);
The constructor (a method inside the class with its name, same as class name) named fresherearth() automatically gets executed. That is the value of a and b gets passed to x and y, the two parameters of the constructor. And the addition result of these two numbers gets initialized to add variable. Finally the value of add gets printed on output using the statement:
System.out.println("\nResult = " +add);
available inside the constructor as its second statement.
Same Program in Other Languages
« Previous Program Next Program »