0% found this document useful (0 votes)
8 views6 pages

Java Arithmetic Calculator Program

This document contains code for 4 Java programs: 1) An arithmetic calculator that takes input from the user and performs basic math operations. 2) An arithmetic calculator that takes command line arguments instead of user input. 3) A program to check if a number is prime or not. 4) A program to implement insertion sort on an integer array.

Uploaded by

rishabh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Java Arithmetic Calculator Program

This document contains code for 4 Java programs: 1) An arithmetic calculator that takes input from the user and performs basic math operations. 2) An arithmetic calculator that takes command line arguments instead of user input. 3) A program to check if a number is prime or not. 4) A program to implement insertion sort on an integer array.

Uploaded by

rishabh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PRACTICAL 1

Java Program to develop ARITHMETIC CALCULATOR :


import [Link].*;
class Arithmetic
{
public static void main(String args[])
{
float a,b,calc;
int p;
Scanner in=new Scanner([Link]);
[Link]("Enter First number = ");
a=[Link]();
[Link]("Enter Second number = ");
b=[Link]();
[Link]("1. Addition.");
[Link]("2. Subtraction.");
[Link]("3. Multiplication.");
[Link]("4. Division.");
[Link]("Enter your choice (1-4) = ");
p=[Link]();
switch(p)
{
case 1 : calc=a+b;
.
[Link]("Addition of two number = "+calc);
.
break;
case 2 : calc=a-b;
.
[Link]("Subtraction of two number = "+calc);
.
break;
case 3 : calc=a*b;
.
[Link]("Multiplication of two number = "+calc);
.
break;
case 4 : if(b==0)
.
{
.
[Link]("Division by zero is not possible.");
.
}
.
else{
.
calc=a/b;
.
[Link]("Division of two number = "+calc);
.
}
.
break;
default : [Link]("Please enter a valid choice !!!.");
}
}
}

OUTPUT : -

PRACTICAL 2

Java Program to develop ARITHMETIC


CALCULATOR using command line arguments
([Link]()):

import [Link].*;
class Calc
{
public static void main(String args[])
{
int a,b,calc,o;
a=[Link](args[0]);
b=[Link](args[1]);
Scanner in=new Scanner([Link]);
[Link]("1. Addition.");
[Link]("2. Subtraction.");
[Link]("3. Multiplication.");
[Link]("4. Division.");
[Link]("Enter your choice(1-4) = ");
o=[Link]();
switch(o)
{
case 1 : calc=a+b;
[Link]("Addition = "+calc);
break;

case 2 : calc=a-b;
[Link]("Subtraction = "+calc);
break;
case 3 : calc=a*b;
[Link]("Multiplication = "+calc);
break;
case 4 : if(b==0)
{
[Link]("Division by zero is not possible");
}
else{
calc=a/b;
[Link]("Division = "+calc);
}
break;
default : [Link]("Please enter valid choice !!");
}
}}

OUTPUT : -

PRACTICAL 3
PROGRAM TO CHECK PRIME NUMBER OR NOT
import [Link];
class prime
{
public static void main(String args[])
{

int i;
boolean flag=false;
[Link]("enter the number to be checked for prime");
Scanner sc=new Scanner([Link]);
int a=[Link]();
for(i=2;i<=a/2;i++)
{
if(a%i==0)
{
flag=true;
}
else
{flag=false;
}
}
if(flag==true)
{
[Link]( " number is not prime");
[Link]( "number is:" +a);
}
if(flag==false)
{
[Link]( ":number is prime");
[Link]( "number is:" +a);
}
}
}

OUTPUT : -

PRACTICAL 4
PROGRAM TO
IMPLEMENT
INSERTION SORT
import [Link];
class insert
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number of elements in the array");

int n=[Link]();
[Link]("enter" +n+ "integers" );
for(int c=0;c<n;c++)
{
int ar[c]=[Link]();
}
for(c=1;c<=n-1;c++)
{d=c;
while(d>0 && ar[d]<ar[d-1])
{
t=ar[d];
ar[d]=ar[d-1];
ar[d-1]=t;
d-}}
[Link]("sorted list in ascending order:\n");
for(c=0;c<=n-1;c++)
{[Link](+ar[c]);}
return 0;
}

OUTPUT : -

You might also like