Calculator Application


Make a Simple Calculator using Command Line Argument in Java.

It will take input from Command Line in series of 
OPERAND – OPERATOR – OPERAND.

Example:

    INPUT : 15 + 29 – 30


    OUTPUT : 14
class first
{
public static void main(String args[])
{
int i=0;
int no;
no =Integer.parseInt(args[i++]);

                while(i<args.length)
{
if(args[i].equals("+"))
{
no = no + Integer.parseInt(args[i+1]);
}
else if(args[i].equals("-"))
{
no = no - Integer.parseInt(args[i+1]);
}
else if(args[i].equals("x"))
{
no = no * Integer.parseInt(args[i+1]);
}
else
{
no = no / Integer.parseInt(args[i+1]);
}
i = i + 2; 
}
System.out.println("\n Calculation : " + no);
}
}

OUTPUT :

E:\>javac first.java

E:\>java first 10 + 20 - 25


 Calculation : 5

No comments:

Post a Comment