Friday, December 30, 2011

Calculator simple calculator using java:JAVA

Save below code as calculator.java

CODE:
import javax.swing.JOptionPane;
public class calculator
{
  public static void main(String args[])
  {
    String s1,s2,s3,n1;
int op2=1;
int a,b,op1,n,i;
float re=0;
while(op2!=0)
{
n1=JOptionPane.showInputDialog("Enter what operation you want to perform using this calculator:\n1.Addition\n2.Substraction\n3.Multiplication\n4.Division\n5.Reminder\nAny other number to exit");
int op=Integer.parseInt(n1);
if((op>0)&&(op<6))
{
 switch(op)
 {
   case 1:
{
 s2=JOptionPane.showInputDialog("Enter number of values you want to add:");
 op1=Integer.parseInt(s2);
 for(i=0;i<op1;i++)
 {
   s3=JOptionPane.showInputDialog("Enter number:");
a=Integer.parseInt(s3);
re=re+a;
 }
 JOptionPane.showMessageDialog(null,"The sum of given "+op1+" values is: "+re,"Addition",JOptionPane.PLAIN_MESSAGE);
 break;
        }
        case 2:
        {
 s2=JOptionPane.showInputDialog("Enter number of values you want to subtract:");
 op1=Integer.parseInt(s2);
 for(i=0;i<op1;i++)
 { 
   if(i==0)
   { 
 s3=JOptionPane.showInputDialog("Enter number:");
 a=Integer.parseInt(s3);
 re=a;
}
            else
            {
     s3=JOptionPane.showInputDialog("Enter number:");
 a=Integer.parseInt(s3);
 re=re-a;
   }
 }
 JOptionPane.showMessageDialog(null,"The differance of given "+op1+" values is: "+re,"Substraction",JOptionPane.PLAIN_MESSAGE);
 break;
}
        case 3:
{
 s2=JOptionPane.showInputDialog("Enter number of values you want to Multiply:");
 op1=Integer.parseInt(s2);
 for(i=0;i<op1;i++)
 {  
   if(i==0)
   { 
 s3=JOptionPane.showInputDialog("Enter number:");
 a=Integer.parseInt(s3);
 re=a;
}
else
{
   s3=JOptionPane.showInputDialog("Enter number:");
a=Integer.parseInt(s3);
re=re*a;
}
 }
 JOptionPane.showMessageDialog(null,"The Product of given "+op1+" values is: "+re,"Multiplication",JOptionPane.PLAIN_MESSAGE);
 break;
        }
case 4:
{
 s2=JOptionPane.showInputDialog("Enter divident value:");
 a=Integer.parseInt(s2);
 s3=JOptionPane.showInputDialog("Enter divisor: ");
 b=Integer.parseInt(s3);
 re=(float)a/b;
 JOptionPane.showMessageDialog(null,"The result of division is: "+re,"Division",JOptionPane.PLAIN_MESSAGE);
 break;
}
        case 5:
{
 s2=JOptionPane.showInputDialog("Enter divident value:");
 a=Integer.parseInt(s2);
 s3=JOptionPane.showInputDialog("Enter divisor: ");
 b=Integer.parseInt(s3);
 re=(float)a%b;
 JOptionPane.showMessageDialog(null,"The result of division is: "+re,"Reminder",JOptionPane.PLAIN_MESSAGE);
 break;
}
      }
    }
    else
    {
      JOptionPane.showMessageDialog(null,"Calculator is terminated as you entered wrong number.","End",JOptionPane.PLAIN_MESSAGE);
 op2=0;
}
    }
  }
}  

//End of program



Output:








To run this java program you should first install jdk(Java Development kit).
you can download it from here.


And also can download this java code and directly execute by simply clicking on .bat file in the rar file downloaded

Password for rar is:letuscodeit


Know about:

No comments:

Post a Comment