Develop a program to do the addition of given two numbers taken from command line in Java

Java programming language is versatile in every aspect, being platform-independent it makes Java a clear cut winner for any developer. The execution of any Java program is hustle-free and precise. We can even pass arguments during execution of a program using the command-line arguments. In this article, you will learn how you could use command-line arguments in Java. Following are the topics discussed in this blog:

What Are Command Line Arguments?

The command-line arguments are passed to the program at run-time. Passing command-line arguments in a Java program is quite easy. They are stored as strings in the String array passed to the args parameter of main() method in Java.

class Example0{ public static void main(String[] args){ System.out.println("edureka" + args[0]); } }

Output:

Develop a program to do the addition of given two numbers taken from command line in Java

To compile and run a java program in command prompt follow the steps written below.

  • Save your program in a file with a .java extension

  • open the command prompt and go to the directory where your file is saved.

  • Run the command – javac filename.java

  • After the compilation run the command – java filename

  • Make sure the Java path is set correctly.

Java Command Line Arguments Example

Here are a few examples to show how we can use the command-line arguments in a Java program.

The beauty relies on the parseInt method in the Integer class. Every Number classes such as Integer, Float, Double and so on have parseXXX methods that convert String into the respective object of their type.

As we all know that array starts its index with zero. Therefore args[0] is the first index in this String[] array which is taken from the console. Similarly, args[1] is second, args[2] is the third element and so on.

When an application is launched, the run-time system passes the command-line arguments to the application’s main method via an array of Strings. 

Factorial of a number using command-line arguments

class Example1{ public static void main(String[] args){ int a , b = 1; int n = Integer.parseInt(args[0]); for(a = 1; a<= n ; a++) { b = b*a; } System.out.println("factorial is" +b); } }

Output:

Develop a program to do the addition of given two numbers taken from command line in Java

Sum of two numbers using command-line arguments

class Example2{ public static void main(String[] args){ int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int sum = a + b; System.out.println("The sum is" +sum); } }

Output:

Develop a program to do the addition of given two numbers taken from command line in Java

Fibonacci Series program using command-line arguments

class Example3{ public static void main(String[] args){ int n = Integer.parseInt(args[0]); int t1 = 0; int t2 = 1; for(int i = 1; i <=n; i++){ System.out.println(t1); int sum = t1+t2; t1 = t2; t2 = sum; } } }

Output:

Develop a program to do the addition of given two numbers taken from command line in Java

Important Points To Remember

  • While launching your application, you can use the command-line arguments to specify the configuration information.

  • When you are using command-line arguments, there is no limitation on the number of arguments. You can use as many as per your requirement.

  • Information in the command-line arguments is passed as Strings.

  • The command-line arguments are stored in the String args of the main() method of the program.

This brings us to the end of this article where we have learned about the Java command line arguments with examples. I hope you are clear with all that has been shared with you in this tutorial.

If you found this article on “Java Command Line Arguments” relevant, check out the Edureka’s Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of “Java Command Line Arguments” and our team will be glad to answer or you can also join our Java Training in Varanasi.

Last update on August 19 2022 21:50:34 (UTC/GMT +8 hours)

Write a Java program to print the sum of two numbers.

In mathematics, summation (capital Greek sigma symbol: ∑) is the addition of a sequence of numbers; the result is their sum or total. The numbers to be summed may be integers, rational numbers, real numbers, or complex numbers.

Pictorial Presentation:

Develop a program to do the addition of given two numbers taken from command line in Java

Sample Solution:

Java Code:

public class Exercise2 { public static void main(String[] args) { System.out.println(24+26); } }

Sample Output:

50

Flowchart:

Develop a program to do the addition of given two numbers taken from command line in Java

Sample solution using input from the user:

Java Code:

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print("Input the first number: "); int num1 = input.nextInt(); System.out.print("Input the second number: "); int num2 = input.nextInt(); int sum = num1 + num2; System.out.println(); System.out.println("Sum: "+sum); } }

Sample Output:

Input the first number: 256 Input the second number: 326 Sum: 582

Flowchart:

Develop a program to do the addition of given two numbers taken from command line in Java

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to print 'Hello' on screen and then print your name on a separate line.
Next: Write a Java program to divide two numbers and print on the screen.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Float vs Double

Both these data types are used to represent floating-point numbers and that double is more precise is a well-known fact.

The use of double over float is highly recommended, especially, if you can't guarantee that your number is within floats' specified range.

But note that double comes with a cost; it is expensive as it takes double the memory space as of what float would take.

Ref: https://bit.ly/2W08jT3

  • Exercises: Weekly Top 12 Most Popular Topics
  • Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • C Programming Exercises, Practice, Solution : For Loop
  • Python Exercises, Practice, Solution
  • Python Data Type: List - Exercises, Practice, Solution
  • C++ Basic: Exercises, Practice, Solution
  • SQL Exercises, Practice, Solution - exercises on Employee Database
  • SQL Exercises, Practice, Solution - exercises on Movie Database
  • SQL Exercises, Practice, Solution - exercises on Soccer Database
  • C Programming Exercises, Practice, Solution : Recursion