Write a program to find sum, difference, product, quotient and remainder of two numbers passed as command line argument |Java program|

Introduction

   In this blog post, we will explore how to create a Java program that computes the sum, difference, product, quotient, and remainder of two numbers passed as command line arguments. This is a basic exercise that demonstrates how to handle command line arguments and perform arithmetic operations in Java.

Code

    
        
        import java.io.*;
        import java.util.*;
        
        public class Operation {
            public static void main(String args[]) throws IOException {
                // Check if two arguments are passed
                if (args.length < 2) {
                    System.out.println("Please enter two values at the run time.");
                    return;
                }
        
                // Parsing command line arguments
                int a = Integer.parseInt(args[0]);
                int b = Integer.parseInt(args[1]);
        
                // Performing arithmetic operations
                int sum = a + b;
                int diff = a - b;
                int mul = a * b;
                int quo = a / b;
                int rem = a % b;
        
                // Displaying the results
                System.out.println("The sum = " + sum);
                System.out.println("The diff = " + diff);
                System.out.println("The product = " + mul);
                System.out.println("The quotient = " + quo);
                System.out.println("The remainder = " + rem);
            }
        }
        
        


To run this program, compile it using the javac command and then execute it with two command line arguments. For example:

    javac Operation.java
    java Operation 4 2


Output


    The sum = 6
    The diff = 2
    The product = 8
    The quotient = 2
    The remainder = 0
        


Explanation

1. Reading Command Line Arguments:
    The program uses the args array to read command line arguments. These arguments are passed as strings, so they need to be converted to integers using Integer.parseInt(). The program also checks if two arguments are provided, and if not, it prompts the user to provide them.
2. Performing Arithmetic Operations:
    The program performs the following arithmetic operations:
  •         Sum: a + b
  •         Difference: a - b
  •         Product: a * b
  •         Quotient: a / b
  •         Remainder: a % b
These operations are straightforward and utilize basic arithmetic operators in Java.
3. Displaying the Results:
    The results of the arithmetic operations are displayed using System.out.println().
Each operation's result is printed with a descriptive message.


Conclusion

     This Java program demonstrates how to handle command line arguments and perform basic arithmetic operations. It covers the following concepts:
  • Parsing command line arguments
  • Performing arithmetic operations
  • Handling and displaying output
This exercise is a great way to get started with Java programming, especially for beginners who are learning how to work with command line arguments and basic arithmetic operations.

Post a Comment

0 Comments