Tuesday, June 7, 2022

How to create a Function to add two numbers in Java? Variable Argument Example Tutorial

How to add two numbers in Java
Hello guys, if you are just starting with Java programming and wondering how to write a function to add two numbers in Java then you have come to the right place. 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  like 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.



Java program to add numbers in Java - Code Example

Here is a 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.
ow to create a Function to add 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 that can accept a variable number of arguments  like. 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


That's all about how to create a function to add two numbers in Java. As you can see you have two approaches to create such function, first is you can create a method with two parameters and just return the addition of those two numbers or you can create a method using variable argument feature of Java which can can create sum of as many number as you want. Just remember that the variable argument parameter must be the last parameter in the method. 


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