Sie sind auf Seite 1von 6

PRACTICAL 1

Java Program to develop ARITHMETIC CALCULATOR :


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

OUTPUT : -

PRACTICAL 2

Java Program to develop ARITHMETIC


CALCULATOR using command line arguments
(Integer.parseInt()):

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

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

OUTPUT : -

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

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

OUTPUT : -

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

int n=sc.nextInt();
Sytem.out.println("enter" +n+ "integers" );
for(int c=0;c<n;c++)
{
int ar[c]=sc.nextInt();
}
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-}}
System.out.println("sorted list in ascending order:\n");
for(c=0;c<=n-1;c++)
{System.out.println(+ar[c]);}
return 0;
}

OUTPUT : -

Das könnte Ihnen auch gefallen