Java Program to find the Sum, Difference, Product, Quotient and Remainder of two numbers passed as Command Line argument
Aim : Write a program to find the sum, difference, product, quotient and remainder of two numbers passed as command line argument.
Code :
Save the file as CommandLine.java
import java.util.*;
import java.io.*;
public class CommandLine
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int x1,x2,opt;
System.out.println("enter 2 numbers");
x1=Integer.parseInt(args[0]);
x2=Integer.parseInt(args[1]);
System.out.println("enter your choice \n1.sum \n2.difference \n3.product \n4.quotient \n5.remainder");
opt=sc.nextInt();
switch(opt)
{
case 1: System.out.println(x1+x2);
break;
case 2: System.out.println(x1-x2);
break;
case 3: System.out.println(x1*x2);
break;
case 4: System.out.println(x1/x2);
break;
case 5: System.out.println(x1%x2);
break;
default: System.out.println("invalid");
}
}
}
Code :
Save the file as CommandLine.java
import java.util.*;
import java.io.*;
public class CommandLine
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int x1,x2,opt;
System.out.println("enter 2 numbers");
x1=Integer.parseInt(args[0]);
x2=Integer.parseInt(args[1]);
System.out.println("enter your choice \n1.sum \n2.difference \n3.product \n4.quotient \n5.remainder");
opt=sc.nextInt();
switch(opt)
{
case 1: System.out.println(x1+x2);
break;
case 2: System.out.println(x1-x2);
break;
case 3: System.out.println(x1*x2);
break;
case 4: System.out.println(x1/x2);
break;
case 5: System.out.println(x1%x2);
break;
default: System.out.println("invalid");
}
}
}
Comments
Post a Comment