Java program to add two numbers - Example code how to

How to add two numbers in Java
In this Java program tutorial, we will add two numbers provided by the user on the command line. Write a Java program to add two numbers in Java is a common programming exercise for programmers and mostly asked in college exams, school homework, and Java courses as well. There are a different variant as well where you need to ask that write a method to add two numbers e.g. sum(int a, int b) and may ask you to get numbers as input from the user. Taking user input is a little bit tricky for Java programmers but once you know how to do that, it's rather simple.

And, if you need to refresh your Data Structure and Algorithms skills to solve those problems then I highly recommend checking out Data Structures and Algorithms: Deep Dive Using Java course on Udemy. It's a hands-on course and covers all essential data structures.


Java program to add numbers in Java - Code Example

How to add two numbers in Java - program example code tutorial
Here is complete Java program to add two numbers in Java. This Java program accepts numbers as user input from the command line using java.util.Scanner class. We have also written an add() method for adding two numbers in Java,  By the way If you know about the variable argument in Java and varargs method, you can even write a method which can accept a variable number of arguments e.g. two, three or four.




package test;

import java.util.Scanner;

/**
 *
 * Java program to add two numbers in Java. numbers should be entered by user
 * in command prompt.
 * @author
 */

public class CollectionTest {

    public static void main(String args[]) {
     
        //create Scanner instance to get input from User
        Scanner scanner = new Scanner(System.in);
     
        System.err.println("Please enter first number to add : ");
        int number = scanner.nextInt();
     
        System.out.println("Enter second number to add :");
        int num = scanner.nextInt();
     
        //adding two numbers in Java by calling method
       int result = add(number, num);
     
       System.out.printf(" Addition of numbers %d and %d is %d %n", number, num, result);
    }
 
    public static int add(int number, int num){
        return number + num;
    }
}
Output
   Please enter the first number to add :
45
Enter the second number to add :
65
 Addition of numbers 45 and 65 is 110



Further Learning
The Complete Java Masterclass
Data Structures and Algorithms: Deep Dive Using Java
Java Fundamentals: The Java Language
Data Structures in Java: An Interview Refresher
Cracking the Coding Interview - 189 Questions and Solutions


Related Data Structure and Algorithm Interview Questions from Javarevisited Blog
  • Top 15 Data Structure and Algorithm Interview Questions (see here)
  • How to find a missing number in an array? (answer)
  • Write a Program remove duplicates from an array without using Collection API? (program)
  • How to reverse String in Java without using API methods? (Solution)
  • Write a method to remove duplicates from ArrayList in Java? (Solution)
  • How to check if a number is binary in Java? (answer)
  • How to compare two arrays in Java? (answer)
  • Top 20 String coding interview questions (see here)
  • Top 30 Array Coding Interview Questions with Answers (see here)
  • How to reverse an array in place in Java? (solution)
  • 50+ Data Structure and Algorithms Problems from Interviews (questions)
  • How to remove duplicate elements from an array in Java? (solution)
  • How to find all pairs whose sum is equal to a given number in Java (solution)
  • Top 30 linked list coding interview questions (see here)
  • Top 50 Java Programs from Coding Interviews (see here)
  • 5 Free Data Structure and Algorithms Courses for Programmers (courses)
  • 10 Algorithms Books Every Programmer Should Read (books)
  • 10 Free Data Structure and Algorithm Courses for Programmers (courses)
  • 100+ Data Structure Coding Problems from Interviews (questions)

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or doubt then please let us know and I'll try to find an answer for you. As always suggestions, comments, innovative and better answers are most welcome.

P. S. - If you are looking for some Free Algorithms courses to improve your understanding of Data Structure and Algorithms, then you should also check the Data Structure in Java free course on Udemy. It's completely free and all you need to do is create a free Udemy account to enroll in this course.

1 comment:

Feel free to comment, ask questions if you have any doubt.