Learn Java practically and Get Certified .

Popular Tutorials

Popular examples, reference materials, learn java interactively, java examples.

Print an Integer (Entered by the User)

  • Add Two Integers

Multiply two Floating Point Numbers

  • Find ASCII Value of a character
  • Compute Quotient and Remainder

Swap Two Numbers

  • Check Whether a Number is Even or Odd
  • Check Whether an Alphabet is Vowel or Consonant

Java Tutorials

  • Java Basic Input and Output
  • Java String format()
  • Java Static Keyword
  • Java Operators
  • Java while and do...while Loop
  • Java Math IEEEremainder()

Java Program to Add Two Integers

To understand this example, you should have the knowledge of the following Java programming topics:

Example: Program to Add Two Integers

In this program, two integers 10 and 20 are stored in integer variables first and second respectively.

Then, first and second are added using the + operator, and its result is stored in another variable sum .

Finally, sum is printed on the screen using println() function.

Sorry about that.

Related Examples

Java Example

Find GCD of two Numbers

Java Tutorial

Java methods, java classes, java file handling, java how to, java reference, java examples, java how to add two numbers, add two numbers.

Learn how to add two numbers in Java:

Add Two Numbers with User Input

Learn how to add two numbers with user input:

Get Certified

COLOR PICKER

colorpicker

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

[email protected]

Top Tutorials

Top references, top examples, get certified.

  • C Program : Remove Vowels from A String | 2 Ways
  • C Program : Sorting a String in Alphabetical Order – 2 Ways
  • C Program : Remove All Characters in String Except Alphabets
  • C Program To Check If Vowel Or Consonant | 4 Simple Ways
  • C Program To Find Reverse Of An Array – C Programs
  • C Program To Print Number Of Days In A Month | Java Tutoring
  • C Program To Check Whether A Number Is Even Or Odd | C Programs
  • C Program To Check A Number Is Negative, Positive Or Zero | C Programs
  • C Program To Count The Total Number Of Notes In A Amount | C Programs
  • C Program Inverted Pyramid Star Pattern | 4 Ways – C Programs
  • C Program To Check Whether A Character Is Alphabet or Not
  • C Program To Input Any Alphabet And Check Whether It Is Vowel Or Consonant
  • C Program To Check If Alphabet, Digit or Special Character | C Programs
  • C Program To Check Character Is Uppercase or Lowercase | C Programs
  • C Program To Check Whether A Year Is Leap Year Or Not | C Programs
  • C Program To Find Maximum Between Three Numbers | C Programs
  • C Program To Check If Triangle Is Valid Or Not | C Programs
  • C Program Area Of Triangle | C Programs
  • C Program To Calculate Profit or Loss In 2 Ways | C Programs
  • C Program Area Of Rectangle | C Programs
  • C Program Find Circumference Of A Circle | 3 Ways
  • C Program Area Of Rhombus – 4 Ways | C Programs
  • Mirrored Rhombus Star Pattern Program In c | Patterns
  • C Program Hollow Diamond Star Pattern | C Programs
  • X Star Pattern C Program 3 Simple Ways | C Star Patterns
  • C Program To Check Number Is Divisible By 5 and 11 or Not | C Programs
  • C Program Area Of Isosceles Triangle | C Programs
  • C Program Area Of Square | C Programs
  • C Program Area Of Trapezium – 3 Ways | C Programs
  • C Program Area Of Parallelogram | C Programs
  • C Program To Find Area Of Semi Circle | C Programs
  • C Program To Find Volume of Sphere | C Programs
  • C Program Check A Character Is Upper Case Or Lower Case
  • Hollow Rhombus Star Pattern Program In C | Patterns
  • C Program to find the Area Of a Circle
  • C Program To Count Total Number Of Notes in Given Amount
  • C Program Area Of Equilateral Triangle | C Programs
  • C Program To Find Volume Of Cone | C Programs
  • C Program To Calculate Volume Of Cube | C Programs
  • C Program To Calculate Perimeter Of Rectangle | C Programs
  • C Program To Calculate Perimeter Of Rhombus | C Programs
  • C Program To Calculate Perimeter Of Square | C Programs
  • C Program Volume Of Cuboid | C Programs
  • C Program Count Number Of Words In A String | 4 Ways
  • C Program Volume Of Cylinder | C Programs
  • C Program To Search All Occurrences Of A Character In String | C Programs
  • C Program To Toggle Case Of Character Of A String | C Programs
  • C Program To Left Rotate An Array | C Programs
  • C Program Inverted Right Triangle Star Pattern – Pattern Programs
  • C Program To Copy All Elements From An Array | C Programs
  • C Program To Delete Duplicate Elements From An Array | 4 Ways
  • C Program To Remove First Occurrence Of A Character From String
  • C Program To Search All Occurrences Of A Word In String | C Programs
  • C Program To Find Reverse Of A string | 4 Ways
  • C Square Star Pattern Program – C Pattern Programs | C Programs
  • C Mirrored Right Triangle Star Pattern Program – Pattern Programs
  • C Program To Delete An Element From An Array At Specified Position | C Programs
  • C Programs – 500+ Simple & Basic Programming Examples & Outputs
  • C Program To Count Occurrences Of A Word In A Given String | C Programs
  • C Program Replace First Occurrence Of A Character With Another String
  • Hollow Square Pattern Program in C | C Programs
  • C Program To Compare Two Strings – 3 Easy Ways | C Programs
  • C Program To Reverse Words In A String | C Programs
  • C Program To Remove Blank Spaces From String | C Programs
  • C Pyramid Star Pattern Program – Pattern Programs | C
  • C Program To Remove Last Occurrence Of A Character From String
  • C Program To Find Last Occurrence Of A Character In A Given String
  • C Program To Trim Leading & Trailing White Space Characters From String
  • C Program To Copy One String To Another String | 4 Simple Ways
  • C Program To Remove Repeated Characters From String | 4 Ways
  • C Plus Star Pattern Program – Pattern Programs | C
  • C Program To Find Last Occurrence Of A Word In A String | C Programs
  • C Program To Find Maximum & Minimum Element In Array | C Prorams
  • C Program Replace All Occurrences Of A Character With Another In String
  • C Program To Sort Even And Odd Elements Of Array | C Programs
  • C Program To Trim White Space Characters From String | C Programs
  • C Program Number Of Alphabets, Digits & Special Character In String | Programs
  • C Program To Trim Trailing White Space Characters From String | C Programs
  • C Program To Check A String Is Palindrome Or Not | C Programs
  • Highest Frequency Character In A String C Program | 4 Ways
  • Rhombus Star Pattern Program In C | 4 Multiple Ways
  • Merge Two Arrays To Third Array C Program | 4 Ways
  • C Program To Count Frequency Of Each Character In String | C Programs
  • C Program To Search An Element In An Array | C Programs
  • C Program Right Triangle Star Pattern | Pattern Programs
  • C Program To Insert Element In An Array At Specified Position
  • C Program To Print All Unique Elements In The Array | C Programs
  • C Program To Find First Occurrence Of A Word In String | C Programs
  • C Program To Count Frequency Of Each Element In Array | C Programs
  • C Program Count Number Of Vowels & Consonants In A String | 4 Ways
  • C Program To Count Occurrences Of A Character In String | C Programs
  • C Program Find Maximum Between Two Numbers | C Programs
  • C Program To Concatenate Two Strings | 4 Simple Ways
  • C Program To Remove First Occurrence Of A Word From String | 4 Ways
  • C Program To Put Even And Odd Elements Of Array Into Two Separate Arrays
  • C Program Hollow Inverted Right Triangle Star Pattern
  • C Program To Count Number Of Even & Odd Elements In Array | C Programs
  • C Program To Count Number Of Negative Elements In Array
  • C Program To Convert Lowercase String To Uppercase | 4 Ways
  • C Program Hollow Inverted Mirrored Right Triangle
  • C Program To Print Number Of Days In A Month | 5 Ways
  • C Program Count Number of Duplicate Elements in An Array | C Programs
  • C Program To Remove All Occurrences Of A Character From String | C Programs
  • C Program To Sort Array Elements In Ascending Order | 4 Ways
  • C Program To Convert Uppercase String To Lowercase | 4 Ways
  • 8 Star Pattern – C Program | 4 Multiple Ways
  • C Program To Input Week Number And Print Week Day | 2 Ways
  • C Program To Right Rotate An Array | 4 Ways
  • C Program To Find Lowest Frequency Character In A String | C Programs
  • Diamond Star Pattern C Program – 4 Ways | C Patterns
  • C Program To Find Sum Of All Array Elements | 4 Simple Ways
  • C Program To Find First Occurrence Of A Character In A String
  • C Program To Read & Print Elements Of Array | C Programs
  • C Program To Sort Array Elements In Descending Order | 3 Ways
  • C Program To Replace Last Occurrence Of A Character In String | C Programs
  • C Program To Find Length Of A String | 4 Simple Ways
  • C Program Hollow Mirrored Rhombus Star Pattern | C Programs
  • C Program Hollow Mirrored Right Triangle Star Pattern
  • Hollow Inverted Pyramid Star Pattern Program in C
  • C Program Half Diamond Star Pattern | C Pattern Programs
  • C Program To Print All Negative Elements In An Array
  • Right Arrow Star Pattern Program In C | 4 Ways
  • C Program Hollow Right Triangle Star Pattern
  • C Program : Capitalize First & Last Letter of A String | C Programs
  • Left Arrow Star Pattern Program in C | C Programs
  • C Program : Check if Two Strings Are Anagram or Not
  • C Program : Check if Two Arrays Are the Same or Not | C Programs
  • C Program Mirrored Half Diamond Star Pattern | C Patterns
  • C Program Inverted Mirrored Right Triangle Star Pattern
  • Hollow Pyramid Star Pattern Program in C
  • C Program : Non – Repeating Characters in A String | C Programs
  • C Program : Sum of Positive Square Elements in An Array | C Programs
  • C Program : Find Longest Palindrome in An Array | C Programs
  • C Program : Maximum Scalar Product of Two Vectors
  • C Program : To Reverse the Elements of An Array | C Programs
  • C Program Lower Triangular Matrix or Not | C Programs
  • C Program Merge Two Sorted Arrays – 3 Ways | C Programs
  • C Program : Find Missing Elements of a Range – 2 Ways | C Programs
  • C Program : Check If Arrays are Disjoint or Not | C Programs
  • C Program : Convert An Array Into a Zig-Zag Fashion
  • C Program : Minimum Scalar Product of Two Vectors | C Programs
  • C program : Find Median of Two Sorted Arrays | C Programs
  • C Program Transpose of a Matrix 2 Ways | C Programs
  • C Program Patterns of 0(1+)0 in The Given String | C Programs
  • C Program : To Find the Maximum Element in a Column
  • C Program : To Find Maximum Element in A Row | C Programs
  • C Program : Check if An Array Is a Subset of Another Array
  • C Program : Rotate the Matrix by K Times | C Porgrams
  • C Program : Non-Repeating Elements of An Array | C Programs
  • C Program To Check Upper Triangular Matrix or Not | C Programs
  • C Program : Rotate a Given Matrix by 90 Degrees Anticlockwise
  • C Program Sum of Each Row and Column of A Matrix | C Programs

Learn Java Java Tutoring is a resource blog on java focused mostly on beginners to learn Java in the simplest way without much effort you can access unlimited programs, interview questions, examples

Java program addition of two numbers – 4 ways | programs.

in Java Programs January 21, 2024 Comments Off on Java Program Addition Of Two Numbers – 4 Ways | Programs

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, 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 .

3. Using classes and Objects

Print addition of numbers : Another sample program using objects and classes

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 .

5. Using Method

Example program #5 : Using method calling to print the addition of two numbers.

6. Using Buffered Reader

To Print addition of numbers using BufferedReader with sample output.

Here is the list of other programs for you:

  • Java program to print the area of a circle
  • Java Program To find the area of a rectangle
  • Java code to print factorial Number

write java program to add two numbers

LCM Of Two Numbers Java Program | 5 Ways – Programs

Java program to find the LCM of two numbers – In the below-mentioned java programs, ...

Javatpoint Logo

Java Tutorial

Control statements, java object class, java inheritance, java polymorphism, java abstraction, java encapsulation, java oops misc.

JavaTpoint

  • Send your Feedback to [email protected]

Help Others, Please Share

facebook

Learn Latest Tutorials

Splunk tutorial

Transact-SQL

Tumblr tutorial

Reinforcement Learning

R Programming tutorial

R Programming

RxJS tutorial

React Native

Python Design Patterns

Python Design Patterns

Python Pillow tutorial

Python Pillow

Python Turtle tutorial

Python Turtle

Keras tutorial

Preparation

Aptitude

Verbal Ability

Interview Questions

Interview Questions

Company Interview Questions

Company Questions

Trending Technologies

Artificial Intelligence

Artificial Intelligence

AWS Tutorial

Cloud Computing

Hadoop tutorial

Data Science

Angular 7 Tutorial

Machine Learning

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures

DAA tutorial

Operating System

Computer Network tutorial

Computer Network

Compiler Design tutorial

Compiler Design

Computer Organization and Architecture

Computer Organization

Discrete Mathematics Tutorial

Discrete Mathematics

Ethical Hacking

Ethical Hacking

Computer Graphics Tutorial

Computer Graphics

Software Engineering

Software Engineering

html tutorial

Web Technology

Cyber Security tutorial

Cyber Security

Automata Tutorial

C Programming

C++ tutorial

Control System

Data Mining Tutorial

Data Mining

Data Warehouse Tutorial

Data Warehouse

RSS Feed

Logo-ProgramIZ

Java Program That Takes Two Numbers As Input And Prints Their Sum

Chapter: Miscellaneous Last Updated: 27-05-2023 12:59:54 UTC

  • When you run this program, it will ask you to enter the two numbers, and once you provide the input, it will display the sum of the two numbers.

1   2  

About Us | Terms & Conditions | Privacy Policy

Copyright © 2021 Programiz.org

Javacodepoint

  • Java Program to Add two Numbers

In this article, you will learn how to write a Java program to add two numbers . Here, you will see multiple solutions for it such as adding or sum of two static numbers , the sum of two dynamic given numbers , and Adding two Numbers using command-line arguments .

Add two Numbers in java

The sum or addition of two numbers in java is very simple. Let’s assume we have two integer numbers x=10 and y=20 . Now we will take a sum integer variable to calculate the sum of x and y. Let’s see the code for it below:

The Sum of two Numbers: 30

Add two dynamically given Numbers in Java

Here we will take two integer numbers from the user dynamically (at run-time). To take input at run-time, we can take the Scanner class object for reading the user’s inputs. The Scanner class is used to get user input, and it is found in the “java.util” package. Let’s see the complete code for it below:

Enter first number: 20 Enter second number: 30 The sum of the given two Numbers: 50

Add two Numbers in Java using command-line Arguments

Now we will see, the sum of two numbers that are given from command line arguments . Generally, the inputs that come from the command line arguments are in String datatype. They are stored as strings in the String array passed to the args[] parameter of the main() method in Java. So we will convert the string to an integer using the parseInt() method before storing it in integer datatype. The command line arguments should be passed to the main() method at run-time.

Let’s see the code for it below:

The sum of given two Numbers: 30

In order to execute the above program, we will open the command prompt and compile it first with the command >javac AddTwoNumbers.java then run the command >java AddTwoNumbers 10 20

You can continue reading, Java Program to Subtract two Numbers

Java logical programs list

Java basic programs.

  • Java Hello world program
  • Java Program to Subtract two Numbers
  • Java Program to Multiply two Numbers
  • Java Program to Check Whether a Number is Even or Odd .
  • Check whether a character is a Vowel or Consonant.
  • Java Program to find Average of Two Numbers
  • Java Program to find Average of Three Numbers
  • Java Program to find the Area of Square
  • Java Program to Calculate Simple Interest
  • Java Program to Calculate Compound Interest
  • List of basic logical programs in Java
  • How to print a Fibonacci series up to a given number in Java?
  • How to calculate the factorial of a given number in java?
  • How to check whether an integer number is a prime number or not in java?
  • How to check whether a number is a perfect number or not in java?
  • How to check whether a number is an Armstrong number or not in java?
  • How to reverse a number in Java?
  • How to check whether a number is a Palindrome number or not in Java?
  • How to swap two numbers without using a third variable in Java?
  • How to reverse a string without using the reverse() method in java?
  • How to check whether a String is a Palindrome or not in java?
  • How to count characters from the string in java?
  • How to check if two strings are anagrams of each other?
  • How to count the occurrence of the given character in a string in java?
  • How to swap two strings without using the third variable in java?

Java Pattern Printing Programs

  • Star pattern programs [Tricks] in Java
  • Top 30-star pattern programs in java
  • Top 10 Number pattern programs in java
  • Top 10 Character pattern programs in java
  • Top 50 Pattern printing programs in java
  • Pyramid Pattern .
  • Diamond Pattern.
  • Hollow Diamond Pattern.

Java Series Printing Programs

  • Print a Fibonacci series
  • Even Numbers Series
  • Odd Numbers Series
  • Square Numbers Series
  • Cube Numbers Series
  • Print Series 1,2,9,28,65
  • Java Program to Print Series 1,2,5,10,17
  • Java Program to Print Series 2,4,8,16,32

Java Array-Based Programs

  • Copy an Array
  • Merge Two Arrays .
  • Sum of Two Arrays.
  • Print Odd/Even No. from an Array .
  • Find the Sum of an Array
  • Sum of Even Numbers in an Array .
  • Average of Odd/Even Numbers in Array.
  • Calculate Average Using Array
  • Find the Smallest Number in an Array .
  • Find the Largest Number in an Array .
  • 2nd Largest Number in an Array .
  • Remove duplicate elements in an Array .

Multi-Dimensional Array Programs

  • Print Matrix or 2D Array .
  • Read a Matrix from the User.
  • Add the Two Matrices .
  • Subtract the Two Matrices .
  • Matrix Multiplication.
  • Identity Matrix Program .
  • Symmetric Matrix Program.
  • Find the Transpose of a Matrix.
  • Find the Smallest Element in a Matrix .
  • Find the Largest Element in a Matrix.
  • Search Matrix.
  • The sum of Diagonals of a Matrix .

Java String Programs

  • String length without using length() method.
  • Count the Number of Vowels in a String .
  • Swap two strings without using the third variable.
  • Reverse a string without using the reverse() method.
  • Reverse the words in a given string sentence
  • Check whether a String is a Palindrome or not in Java .
  • Count characters from the string in Java .
  • Count the number of words in the String.
  • Check if the two strings are anagrams of each other .
  • Count the occurrence of the given character in a string.
  • Find the Maximum Occurring Character in a String
  • Find the First non-repeating Character in a given String
  • Remove all Duplicates from a given String
  • Remove a given character from the String
  • Find the Sum of Digits in a String.
  • Compare two Strings in Java.
  • String Permutations Program in Java
  • Combinations of String in Java

Java Conversion Programs

  • Binary to Decimal
  • Binary to Decimal using Recursion
  • Decimal to Binary
  • Decimal to Octal
  • Decimal to Hexadecimal
  • Fahrenheit to Celsius
  • Celsius to Fahrenheit

Java Miscellaneous Programs

  • Generate Random String in Java
  • Generating Random Numbers in Java
  • Generating OTP in Java
  • Secret Message Program in Java

Java Game Programs

  • Tic-Tac-Toe Game in Java
  • Hangman Game in Java
  • Rock Paper Scissors Game in Java
  • Number Guessing Game in Java

Java Programs based on the Collection Framework

  • ArrayList Manipulation: add, remove Elements, find size
  • Write a Java program to reverse a List without using the built-in Collections.reverse() method.
  • Find duplicate elements in an ArrayList
  • Implement a Java program to sort an ArrayList of integer numbers.
  • Implement a Java program to sort an ArrayList of custom objects based on a specific attribute.
  • Create a stack using an ArrayList and implement push, pop, and peek operations.
  • Convert an ArrayList of strings into a string array.
  • Write a Java program to remove all elements from an ArrayList that are greater than a certain value.
  • Write a Java program to remove duplicates from an ArrayList while preserving the original order.
  • Write a Java program to find the common elements in two given ArrayLists.
  • ArrayList partition into two separate lists of odd/even numbers.
  • Write a program that extracts a sublist from an ArrayList, given a start and end index.
  • Merge two sorted ArrayLists into a single sorted ArrayList.
  • Find unique string in ArrayList with its frequency
  • Write a program to swap the positions of two elements in an ArrayList given their indices.
  • Write a program to Create a Custom LinkedList in Java
  • Write a Java program for Basic Queue Implementation using LinkedList
  • Write a Java program to create a PriorityQueue to manage a to-do list

Leave a Reply Cancel reply

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

Save my name, email, and website in this browser for the next time I comment.

PrepBytes Blog

ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING

Sign in to your account

Forgot your password?

Login via OTP

We will send you an one time password on your mobile number

An OTP has been sent to your mobile number please verify it below

Register with PrepBytes

Java program to add two numbers.

' src=

Last Updated on May 9, 2023 by Prepbytes

write java program to add two numbers

You will learn how to create a Java program that adds two integers in this tutorial. This is a very simple Java program that asks the user to enter two integers, saves those inputs in two different variables, and then shows the total of the two values.

To determine the sum of two integer numbers provided by the user, we shall create two variables. The user is prompted to enter two integer values in the first variable, after which the variable shows the total of the two integers. Using a user-defined function, we are carrying out the identical action in the second C program.

What is Addition of Two Numbers in Java?

In the Java programming language, adding two integers entails taking two integer numbers, adding them, and then printing the result. Let’s look at the Java programming language’s technique for adding two numbers, which is a straightforward process.

Algorithm for Addition of Two Numbers in Java

  • Declare two variables.
  • Initialize the variables with some values.
  • Declare another variable to store the sum of the numbers.
  • Apply the mathematical plus(+) operator between two values and store the result.

Method 1: Standard Program for the Addition of Two Numbers in Java

This method of computing the sum of two numbers in Java is simple. Let’s look for the sum of two numbers in Java using methods.

There are two ways to find the addition of two numbers in java using the method

  • By using the user-defined method
  • By using the sum() method

Method 2: Sum of Two Numbers in Java by using the User-defined Method

Java allows the reading of user input. The user-defined method sum() can accept two numbers from the user as input. The Java program’s implementation to add two numbers is shown below.

Method 3: Addition of Two Numbers in Java by using Integer.sum() Method

The sum() method is available from the integer class. A static method called sum() adds two numbers according to the operator.

Method 4: Addition of Two Numbers in Java by using Command Line Arguments

Another method to taking input from user is command line arguments. We will discuss the java program to add two numbers where both the numbers are provided by the user using arguments from the command line.

Explanation: Compile the above program using the command javac SumofNumbers.java. After that, run the program by using command java SumofNumbers 50 50. Where 50 and 50 are command line arguments.

Conclusion: We had discussed four different approaches for the Java program to add two numbers. Along with that we have also learned how to take input from users. Addition of two numbers in Java is a simple program, but there are many different approaches for the addition of two numbers in Java. You have to figure out more approaches for the addition of two numbers in Java.

FAQ For Java Program To Add Two Numbers

Q1. What is sum += in Java? Ans. Java uses the compound addition assignment operator, which looks like: += (plus sign, followed by equal sign). sum += 10 ; // Add 10 to sum.

Q2. What is a keyword in Java? Ans. A Java keyword is one of 50 reserved terms that have a special function and a set definition in the Java programming language. The fact that the terms are reserved means that they cannot be used as identifiers for any other program elements, including classes, subclasses, variables, methods and objects.

Q3. What is the method in Java? Ans. A method in Java is a block of code that, when called, performs specific actions mentioned in it. For instance, if you have written instructions to draw a circle in the method, it will do that task. You can insert values or parameters into methods, and they will only be executed when called.

Q4. What is a class in Java? Ans. A class in Java is a logical template to create objects that share common properties and methods. Hence, all objects in a given class will have the same methods or properties. For example: in the real world, a specific cat is an object of the “cats” class.

Other Java Programs

Java Program to Check Prime Number Java Program to Check Whether a Number is a Palindrome or Not Java Program to Find the Factorial of a Number Java Program to Reverse a Number Java Program to search an element in a Linked List Program to convert ArrayList to LinkedList in Java Java Program to Reverse a linked list Java Program to search an element in a Linked List

Leave a Reply Cancel reply

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

Save my name, email, and website in this browser for the next time I comment.

  • Linked List
  • Segment Tree
  • Backtracking
  • Dynamic Programming
  • Greedy Algorithm
  • Operating System
  • Company Placement
  • Interview Tips
  • General Interview Questions
  • Data Structure
  • Other Topics
  • Computational Geometry
  • Game Theory

Related Post

Java interview questions, concrete class in java, what is the difference between interface and concrete class in java, taking character input in java, difference between list and set in java, java 8 stream.

Codeforwin

Java program to add two numbers and print result

Write a Java program to input two numbers add two numbers. In this example we will learn to use arithmetic operator to find sum of two numbers. We will learn to input two number from user and find sum of two numbers in Java.

Required knowledge

Basic input/output, Arithmetic operator

This example does not require any special Java knowledge. You just need to know how to read input from user .

Program to add two numbers and print result

Note: We use System.out.println() to print string on console and move to next line. Whereas System.out.print() print string and stays in the same line.

Happy coding 😉

Java Program to Perform Addition, Subtraction, Multiplication and Division

Java programming examples tutorial index.

Java program to perform basic arithmetic operations of two numbers. Numbers are assumed to be integers and will be entered by the user.

This Java program asks the user to provide integer inputs to perform mathematical operations.

  • Scanner class and its functions are used to obtain inputs, and println() function is used to print on the screen.
  • Scanner class is a part of java.util package , so we required to import this package in our Java program.
  • We also required to create a object of Scanner class to call its functions.

Program Output:

JavaProgramTo.com

Spring boot.

  • Collections
  • Java Programs
  • Troubleshoot

Header$type=social_icons

$show=/label, java program to add two numbers (scanner) for freshers.

Twitter

1. Overview In this tutorial, You'll learn writing a java program to add two numbers for freshers or fresh graduates. This program ...

1. Overview

Java Program To Add Two Numbers

2. Sum of two numbers example in java

3. another famous example on sum of two numbers with scanner, 4. string is provided to the nextint() method. what is output now, 5. conclusion.

write java program to add two numbers

Java 8 Tutorial

  • Java 8 New Features
  • Java 8 Examples Programs Before and After Lambda
  • Java 8 Lambda Expressions (Complete Guide)
  • Java 8 Lambda Expressions Rules and Examples
  • Java 8 Accessing Variables from Lambda Expressions
  • Java 8 Method References
  • Java 8 Functional Interfaces
  • Java 8 - Base64
  • Java 8 Default and Static Methods In Interfaces
  • Java 8 Optional
  • Java 8 New Date Time API
  • Java 8 - Nashorn JavaScript

Java Threads Tutorial

  • How To Create Thread?
  • How To Kill Thread?
  • Thread Priorities
  • Thread States Life Cycle Flow
  • Thread Join()
  • interrupt() VS interrupted() VS isInterrupted()
  • Create Thread Without Implementing Runnable
  • Create Thread Without Extending Thread Class
  • Matrix Multiplication With Thread (Efficient Way)
  • How To Delay Thread Execution?

Kotlin Conversions

  • Error Stack Trace To String
  • String To Date
  • List To Array
  • Convert String to Float
  • Convert List to Map
  • Convert Map to List

Kotlin Programs

  • Sort ArrayList of Custom Objects
  • Join Two Lists
  • Get Current Date and Time

Java Conversions

  • Java 8 List To Map
  • Java 8 String To Date
  • Java 8 Array To List
  • Java 8 List To Array
  • Java 8 Any Primitive To String
  • Java 8 Iterable To Stream
  • Java 8 Stream To IntStream
  • String To Lowercase
  • InputStream To File
  • Primitive Array To List
  • Int To String Conversion
  • String To ArrayList

Java String API

  • chars() - Java 9
  • codePointAt()
  • codePointCount()
  • codePoints() - Java 9
  • compareTo()
  • compareToIgnoreCase
  • contentEquals()
  • copyValueOf()
  • describeConstable() - Java 12
  • equalsIgnoreCase()
  • indent() - Java 12
  • isBlank() - java 11
  • lastIndexOf()
  • offsetByCodePoints()
  • regionMatches()
  • replaceFirst()
  • replaceAll()
  • resolveConstantDesc()
  • strip(), stripLeading(), stripTrailing()
  • substring()
  • toCharArray()
  • toLowerCase()
  • transform() - Java 12
  • @EnableAutoConfiguration Annotation
  • MongoDB - CRUD Operations
  • MongoDB - Projections and Aggregations
  • Scanning Multiple Packages

$show=Java%20Programs

$show=kotlin.

addition of two numbers in Java using method

 addition of two numbers in Java using method

  • On September 10, 2021
  • By Karmehavannan
  • Categories: addition , Calculations
  • Tags: Java language , operator

addition of two numbers in Java using methods

In this tutorial, we will discuss the addition of two numbers in Java using the Java method.

In this topic, we will learn a simple concept of how to add two number in Java programming language using the Java method.

already we will know the same concept using the operator in a simple way.

if you known click here Java program to sum of two numbers

This program has following procedures to completion

  • class and main method
  • get input from user – using Scanner class
  • method definition – user-define method
  • calling the method
  • Display method on the screen

Calculate the total value of the two integer number

Calculate the total value of two integer number using the method.

When the above code is compiled and executed, it produces the following results

Calculate total value of two integer number using the method with the return value

In this program, we can denoted some impotent steps to completed this program

  • return value to main method

Calculate total value of two floating point number

Calculate the total value of the two floating point number using the method.

Suggested for you

Operator in Java

Data type of Java

Method in Java language

Related posts:

  • Previous Post

' src=

Fla Cis August 1, 2019 at 8:40 am

I want to add 3 double numbers for example : 4.67, 8.93, 10.00. if result is natural number return 1 else return 0. what is the condition? can you help me?

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

Notify me of follow-up comments by email.

Notify me of new posts by email.

Recent post

  • Python Full Pyramid star pattern program
  • Write a function or method to convert C into F -Entered by user
  • How to write a function or method to convert Celsius into Fahrenheit
  • Program for Celsius into Fahrenheit conversion
  • Program to convert Celsius into Fahrenheit

Most Popular

Python program to calculate electricity bill

dumb it dude cover pic

  • Java / Java Programs

Java GUI Program to Add Two Numbers Using AWT

by Scottshak · Published July 11, 2017 · Updated September 17, 2018

Remember that program we did to add two numbers ? We are going to simply uptrade! We will create a Java GUI program to add two numbers using AWT and it’s gonna be fun.

Adding two numbers doesn’t have too much of a logic. But when you are doing so while using AWT things become a little challenging. Snice a text field in Java takes in String as input we need to first parse it into the form of Integer .

We are going to create two text fields and a label where the output will be seen. A button will be used to trigger the event.

So let’s get to it.

Here is the addition program:

As you can see our main code went into the addActionListener method. We created three variables and parsed the String values into integers. Then for setText we had convert them back to String, and so String’s valueOf() came to the rescue.

Notice, since you are using AWT classes and event handler you need to import the respective classes first or your code wouldn’t work.

Now let’s run the above program.

You will get the following result when you run it:

Java GUI program to add two numbers using AWT

Isn’t that a charm?

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to print (Opens in new window)

Tags: add two numbers addition program addition program in java addition program in java gui awt addition program easy java easy java tutorials how to make a gui in java java awt java for beginners java for dummies java gui Java GUI program to add two numbers Java GUI program to add two numbers using AWT java made easy java program to add two numbers java programming to add two numbers learn java program to add numbers program to add two numbers

' src=

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

  • Next story  Count Characters in String Java GUI AWT | How to Count Words
  • Previous story  How to Get the IP Address of a Website using Java GUI AWT

You may also like...

How to read from XLSX file in Java using Apache POI

How to Read XLSX File in Java using Apache POI

March 6, 2018

 by Scottshak · Published March 6, 2018 · Last modified August 22, 2018

write java program to add two numbers

Understanding Static Class in Java | Java Nested Class

April 23, 2017

 by Scottshak · Published April 23, 2017 · Last modified September 3, 2019

write java program to add two numbers

Java Loop | For Loop Java | While and Do-While Loop

May 15, 2017

 by Scottshak · Published May 15, 2017 · Last modified June 30, 2017

9 Responses

  • Pingbacks 3

' src=

“b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(tf1.getText()); int b = Integer.parseInt(tf2.getText()); int c = a + b; l1.setText(“Their sum is = ” + String.valueOf(c)); “”

if i want to add more than one button such as “add” ,”sub” ,”mul” etc.. so what is step to add more button

' src=

Initialize a new button like:

Button b2 = new Button(“LABEL OF THE BUTTON”);

and then set its bounds by using:

b2.setBounds(150,250,80,70);

Then finally use

to include it.

Eventually, you have to create a new Action Listener Anonymous class

b2.addActionListener(new ActionListener(){

and then specify what should happen when you click the button inside a method.

– Scottshak

' src=

How to change the frame location and Setdefaultclose operation

Hi Santhosh,

If you want to bring it to the centre you can try:

setLocationRelativeTo(null);

set default close operation can be performed by:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Hope that helps.

' src=

[…] Also, you could use Java GUI or AWT to make an interesting looking program. I have created an addition program using GUI here. […]

[…] How to Add Two Numbers […]

[…] a Checkbox in Java GUI is quite a simple process. Here, unlike the buttons we saw in a Java GUI program to add two numbers there is no addActionListener. We have addItemListener instead. Checkbox in Java GUI comes really […]

Leave a Reply Cancel reply

Java tutorials, chapter 1 - how to install java, chapter 2 - java demo program, chapter 3 - classes, objects and methods, chapter 4 - taking user inputs, chapter 5 - reading from a text file, chapter 6 - writing to a text file, chapter 7 - static class, chapter 8 - constructors, chapter 9 - inheritance, chapter 10 - super keyword, chapter 11 - this keyword, chapter 12 - if else, chapter 13 - loops, chapter 14 - array, chapter 15 - abstraction, chapter 16 - interface, chapter 17 - exception handling, chapter 18 - polymorphism, chapter 19 - encapsulation, chapter 20 - multithreading, chapter 21 - synchronization, chapter 22 - anonymous inner class, chapter 23 - string, chapter 24 - stringbuffer and stringbuilder, chapter 25 - formatting a string, chapter 26 - regex, chapter 27 - object, chapter 28 - collections | jcf - 1, chapter 29 - queue | jcf - 2, chapter 30 - set | jcf - 3, chapter 31 - map, chapter 32 - socket programming, chapter 33 - urlconnection, chapter 34 - gui, recommended.

  • Recent Posts
  • Popular Posts

how to perform sorting for numbers in javascript featured image

How to Perform Sorting of Numbers in Javascript

how to upload file in Playwright featured image

How to Upload a File in Playwright

How to Generate Allure reports in Azure Pipeline with Playwright Featured Image

How to Generate Allure Reports in Azure Pipeline with Playwright

Featured Image for MFA Automation Playwright for Azure AD Microsoft

How to automate Microsoft MFA using Playwright

how to delete a branch on bitbucket

How to delete a branch in Bitbucket

write java program to add two numbers

Solving “An internal error occurred during Launching TestNGFile” Problem in Selenium

Mailing Reports in Jenkins

How to Mail Reports Through Jenkins | Configuring Email Notifications

image for How to Share Youtube Videos in Full Thumbnail Size on Facebook

How to Share Youtube Videos in Full Thumbnail Size on Facebook

How to Upload a File in Jenkins file parameter cover

How to Upload a File in Jenkins | Using File Parameter

write java program to add two numbers

How to Use Classes in Java | Object and Methods

About This Site

Subscribe to blog via email.

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Email Address

Selenium Tutorials

1. selenium webdriver, 3. css selectors, 4. selenium commands, 5. click and links, 6. refresh a page, 7. wait commands, 8. find number of frames, 9. handle frames, 10. window handling, 11. handling notifications, 12. extent reports, 13. read excel file, 14. write in excel file, 15. batch file, java programs, first java demo program, add two numbers, add two numbers using gui, count characters in string, quiz program, last word from url, encode and decode message, read xml using jsoup, split a string from comma.

Like us on Facebook

Follow me on Twitter

  • 90% Refund @Courses
  • Java Arrays
  • Java Strings
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Spring Boot

Related Articles

  • Solve Coding Problems
  • Java Program to illustrate Total Marks and Percentage Calculation
  • Java Program to Extract Last two Digits of a Given Year
  • Java Program to Demonstrate the Call By Value
  • Java Program to check Armstrong Number
  • Java Program to Round a Number to n Decimal Places
  • Java Program to Implement Playfair Cipher Algorithm
  • Java Program to Compute the Sum of Diagonals of a Matrix
  • Java Program to Convert Binary String to Decimal Using Wrapper Class
  • Convert Long Values into Byte Using Explicit Casting in Java
  • Encrypt PDF using Java
  • Java Program to Store Escape Sequence Using Character Literals
  • Java Program to Calculate Sum of First N Natural Numbers
  • How to Generate Unique Positive Long Number in Java?
  • Java Program to Extract Content From a XML Document
  • Java Programs - Java Programming Examples
  • Java Program to Calculate Sum of Two Byte Values Using Type Casting
  • Getting Your Own Device IP Address using Java
  • Drawing a Line in a PDF Document using Java
  • Creating a User-Defined Printable Pair Class in Java

Java Program to Add Two numbers Without using Arithmetic Operator

Here, we need to write a function that returns the sum of two stated integers. And the function must not utilize any of the arithmetic operators such as +, ++, –, -, .. Etc.). It will be a very basic elementary level program to compute the sum by simply using the ‘+’ operator and thereby simply printing and displaying out the resultant. But if we refrain from constraint not to use any of arithmetic operators then there is only one way out which is with the use of XOR operator which can perform an addition operation for two given bits. And for the carry bit , we can use AND (&) operator.

It is done using the bit-wise operators . Here we are going to use mainly three operations as follows:

  • Bit-wise XOR
  • Bit-wise AND
  • Bit-wise left shift operator

First, the numbers are converted into binary format. Considering 8 indices for an integer data type.  Now the carry is handled by a bit-wise left shift operator and the rest of the binary is displayed on the screen as an integer number depicting the sum of the above two integers. It is depicted below: 

Input  : 1, 2 Output : 3 Explanation:  a = 1 : 00000001 b = 2 : 00000010 —————- c = 3 : 00000011  

Implementation:

Time Complexity : O(log 2 (b)) Auxiliary Space : O(1)

Please Login to comment...

  • ManasChhabra2
  • simranarora5sos

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

Computer Revolution (www.comrevo.com)

Search this website.

  • Cloud Computing
  • Web Designing
  • Data Structures
  • Multi-Threading
  • Blogging Tips
  • YouTube Tips
  • Subscribe Youtube Channel
  • Amazon Links
  • Udemy Courses

Tuesday 22 August 2017

Java swing program to add two numbers | java swing program for addition of two numbers.

write java program to add two numbers

No comments:

Post a comment.

Java swing program to add two numbers

In this program, You will learn how to add two numbers using swing in java.

Java swing program to add two numbers

Example: How to add two numbers using swing in java.

IMAGES

  1. Java Program to Add Two Numbers

    write java program to add two numbers

  2. Java Example Program to Add Two Numbers ( User Input )

    write java program to add two numbers

  3. Java Programming Tutorials for Beginners

    write java program to add two numbers

  4. [Java Basics] Few more example programs to Kick start with Java : Addition of two numbers

    write java program to add two numbers

  5. Java Program to Add Two Binary Numbers

    write java program to add two numbers

  6. Java program to add two numbers

    write java program to add two numbers

VIDEO

  1. 020 number system in java

  2. write java program that display prime numbers

  3. write a Java program to check a number even or odd

  4. C Program to Add Two Integers For User Input For Visual Studio Code (VS Code) #shorts #coding

  5. Add Two Number in Java

  6. addition of two numbers in interface || Java || beginners #youtube #coding

COMMENTS

  1. Java Program to Add Two Integers

    Enter two numbers 10 20 The sum is: 30. In this program, two integers 10 and 20 are stored in integer variables first and second respectively. Then, first and second are added using the + operator, and its result is stored in another variable sum. Finally, sum is printed on the screen using println() function.

  2. Java How To Add Two Numbers

    W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

  3. Java Program to Add Two Numbers

    Program to Add Two Numbers in Java. Below is the implementation of adding two Numbers are mentioned below: Java. import java.io.*; class GFG {. public static int sum (int num1, int num2) {. return num1+num2; }

  4. Java Program Addition Of Two Numbers

    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 ...

  5. Java Program to Add two Numbers

    In this tutorial, you will learn how to write a Java program to add two numbers.We will see three programs: In the first program, the values of the two numbers are given. In the second program, user is asked to enter the two numbers and the program calculates the sum of the input numbers.

  6. Sum of Numbers in Java

    In Java, finding the sum of two or more numbers is very easy. First, declare and initialize two variables to be added. Another variable to store the sum of numbers. Apply mathematical operator (+) between the declared variable and store the result. The following program calculates and prints the sum of two numbers.

  7. Java Program That Takes Two Numbers As Input And Prints Their Sum

    Similarly, we prompt for the second number and store it in number2. Then, we calculate the sum of the two numbers by adding number1 and number2, and store the result in the variable sum. Finally, we print the sum to the console using System.out.println (). When you run this program, it will ask you to enter the two numbers, and once you provide ...

  8. Java Program to Add Two Numbers

    Write a Java Program to Add Two Numbers and print the output. It is one of the fundamental concepts of calculations, and to achieve the same, we can use the arithmetic operator (+). This simple Java program lets the user enter two integer values and then add those two numbers using the arithmetic operator (+) and assign them to a variable sum.

  9. Java Program to Add two Numbers

    In this article, you will learn how to write a Java program to add two numbers.Here, you will see multiple solutions for it such as adding or sum of two static numbers, the sum of two dynamic given numbers, and Adding two Numbers using command-line arguments.. Add two Numbers in java. The sum or addition of two numbers in java is very simple. Let's assume we have two integer numbers x=10 and ...

  10. Java Program to Add Two Integers

    Program output. The sum of both integers: 30. 2. Java program to add two integers entered by user. In the given Java example, we will take the two inters inputs from the user and after adding both integers, we will store their sum in the variable sum. Finally, the sum has been printed in the screen with the System.out.println statement.

  11. Java Program to Add Two Numbers

    Method 4: Addition of Two Numbers in Java by using Command Line Arguments. Another method to taking input from user is command line arguments. We will discuss the java program to add two numbers where both the numbers are provided by the user using arguments from the command line. java. class SumOfNumbers4.

  12. Java Program to Add Two Integers

    Program Output: Enter an integer: 15 Enter a second integer: 5 The sum of two integers 15 and 5 is: 20. This Java program adds two integers. It takes input from the user in the form of an integer and performs simple arithmetic operations for addition.

  13. Java program to add two numbers and print result

    Note: We use System.out.println () to print string on console and move to next line. Whereas System.out.print () print string and stays in the same line. Output. Enter first number : 10 Enter second number : 5 Sum of 10 and 5 is : 15.

  14. Java Program to Perform Addition, Subtraction ...

    Java Program to Perform Addition, Subtraction, Multiplication and Division. Java program to perform basic arithmetic operations of two numbers. Numbers are assumed to be integers and will be entered by the user. This Java program asks the user to provide integer inputs to perform mathematical operations. Scanner class and its functions are used ...

  15. Java Program To Add Two Numbers (Scanner) For Freshers

    Here taken two int type variables number1 and number2 which stores the values 10 and 20. Then, using arithmetic formula number1 + number2 using '+' operator and the produced result is stored in the int type sum variable. Finally, result is printed on the console using System.out.println() method. 3. Another famous Example on Sum of two numbers with Scanner

  16. addition of two numbers in Java using method

    Enter the first number: 55. Enter the second number: 65. Sum of two numbers 120. In this program, we can denoted some impotent steps to completed this program. class and main method. get input from user - using Scanner class. method definition - user-define method. calling the method. return value to main method.

  17. Java Generics and adding numbers together

    A way to sum two items. In Java, you do it through an interface. Here is a complete example: import java.util.*; interface adder<T extends Number> { T zero (); // Adding zero items T add (T lhs, T rhs); // Adding two items } class CalcSum<T extends Number> { // This is your method; it takes an adder now public T sumValue (List<T> list, adder<T ...

  18. Java GUI Program to Add Two Numbers Using AWT

    We will create a Java GUI program to add two numbers using AWT and it's gonna be fun. Adding two numbers doesn't have too much of a logic. But when you are doing so while using AWT things become a little challenging. Snice a text field in Java takes in String as input we need to first parse it into the form of Integer.

  19. Java Program to Add Two numbers Without using Arithmetic Operator

    Java Program To Add Two Numbers Represented By Linked Lists- Set 1. Java Program to Add Two Numbers. Multiples of 3 and 5 without using % operator. Java Program to Swap two Strings Without Using any Third Variable. Program to add and Subtract Complex Numbers using Class in Java. Java Program to Add Two Matrix Using Iterative Approach.

  20. Java Swing Program To Add Two Numbers

    Java Swing Program To Add Two Numbers | Java Swing program For Addition of Two Numbers; How To Play Videos in Airtel Xstream Set Top Box with Pen Drive | Airtel Xstream Smart Box USB Use; Mapping Techniques for Load Balancing | Static, Dynamic, Block Distribution, Cyclic, Block Cyclic

  21. java Servlets addition of 2 numbers

    Learn how to perform a simple addition of two numbers in Java servlets with a clear example and explanation. Find out how to use request parameters, response writers, and HTML forms to create a dynamic web application.

  22. Java swing program to add two numbers

    In this program, You will learn how to add two numbers using swing in java. class Test extends JFrame implements ActionListener { JButton jb1; JTextField jt1, jt2; JLabel lbl; } Example: How to add two numbers using swing in java.

  23. Java Program to Add two Binary Numbers

    Enter first binary number: 11100 Enter second binary number: 10101 Output: Same program in Eclipse IDE: Output of the program in Eclipse: Here are a few related java examples: - Chaitanya. In this tutorial we will write a java program to add two binary numbers. Binary number system has only two symbols 0 & 1 so a binary numbers consists.