blueJ project

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 117

PROGRAM: 1

AIM:
Write a program in java to print the SMITH number.
ALGORITHM:
1) Start the program
2) Import the header file

import java.util.Scanner;

3) Declare the class object and main function

public class SmithNo

4) Declare the variables

5) Long num,savNum;
int digit,sumDigit=0,sumPrime=0,factor,
countOfFactors=0,prodFactors=1,i;

6) Accept the value from the user

num=in.nextLong();

7) Find the sum of the digits

Digits=(int)savNum%10;
sumDigit+=digit;
savNum/=10;

8) Find the sum of the prime factors

Digit=(int)savNum%10;
sumPrime+=digit;
savNum/=10;

9) Repeat the steps 6 & 7 till savNum comes to 0

a. Check whether sumDigit is equal to sumPrime and print SMITH


NUMBER
if(sumPrime==sumDigit)
System.out.println(“IS SMITH NUMBER”);
else

System.out.println(“NOT A SMITH NUMBER”);


10) End the program
CODING:
import java.util.Scanner;
public class SmithNo{
public static void main(String arg[]){
Scanner in=new Scanner(System.in);
long num,savNum;
System.out.println(“Enter a number”);
num=in.nextLong();
int
digit,sumDigit=0,sumPrime=0,factor,countOfFactors=0,prodFactors=1;
for(int i=1;i<=num;i++)
if(num%i==0) countOfFactors++;
if(countOfFactors>2){
savNum=num;
System.out.println(“Sum of digits”);
while(savNum>0){
digit=(int)savNum%10;
sumDigit+=digit;
savNum/=10;
System.out.print(digit+”,”);}
System.out.println(“-) is : “+sumDigit);
factor=2;
long Num2=num;
System.out.println(“Sum of prime factors(“);
while(factor<=Num2){
if(Num2%factor==0){
System.out.print(factor+”,”);
If(factor<10){
sumPrime+=factor;}
else{
savNum=factor;
while(savNum>0){
digit=(int)savNum%10;
sumPrime+=digit;
savNum/=10;}}
Num2/=factor;}
else{
if(factor==2) factor=3;
else factor+=2;}}
System.out.println(“-)is : “+sumPrime);
if(sumPrime==sumDigit)
System.out.println(“IS SMITH NUMBER”);
else
System.out.println(“NOT A SMITH NUMBER”);}
else
System.out.println(“It is a prime number. A prime number can not be a Smith
Number.”);}}
RESULT: The above java program successfully executed.
PROGRAM: 2
AIM:
Write a program in java to print the CATLAN number
ALGORITHM:
1) Start the program
2) Import the header file

Import java.util.Scanner;

3) Declare the class object and main function

public class Catlan

4) Declare the variables

int n
long n1,n2,a;
long CatNumber;

5) Accept the value of n

n=in.nextInt();

6) Check if n is between 1 & 10

if(n>=1&&n<=10)

7) Find the factorial of a and 2a

longfactOfa=1,factOf2a=1;
for(long i=1;i<=a;i++)
factOfa*=i;
for(long i=1;i<=2*a;i++)
factOf2a*=i;

8) Repeat the step 7 till a comes equal to n


9) Check and print the catlan number
CatNumber=factOf2a/((factOfa*factOfa)*(a+1));
System.out.println(“ “+cnt++ +”.”+CatNumber);

10) End the program


CODING:

import java.util.*;
public class Catlan{
public static void main(String ads[]){
Scanner in=new Scanner(System.in);
System.out.println(“Enter the value of n(1……10):”);
int n=in.nextInt();
long n1,n2,a;
long CatNumber;
intcnt=1;
if(n>=1&&n<=10){
for(a=1;a<=n;a++){
longfactOfa=1,factOf2a=1;
for(long i=1;i<=a;i++)
factOfa*=i;
for(long i=1;i<=2*a;i++)
factOf2a*=i;
CatNumber=factOf2a/((factOfa*factOfa)*(a+1));
System.out.println(“ “+ cnt++ +”.”+CatNumber);}}}}

RESULT: The above java program successfully executed.


PROGRAM: 3
AIM:
Write a program in java to print the type of number DEFICIENT NUMBER,
PERFECT NUMBER, ABUNDANT NUMBER
ALGORITHM:
1) Start the program
2) Import the header file

import java.util.Scanner;

3) Declare the class object and main function

public class TestNumber

4) Declare the variables

int n;
int s=0;

5) Accept the value of n

n=in.nextInt();

6) Check if n is greater than 0

if(n>0)

7) Find the divisors of n

if(n%i==0)
s+=i;

8) Repeat the step 7 till i comes equal to n


9) Check and print the required type of number

if(s<2*n)
System.out.println(n+” is a DEFICIENT NUMBER.”);
else if(s==2*n)
System.out.println(n+” is a PERFECT NUMBER.”);
else
System.out.println(n+” is an ABUNDANT NUMBER.”);
10) End the program

RESULT: The above java program successfully executed.


CODING:
import java.util.*;
public class TestNumber{
public static void main(String srev[]){
Scanner in=new Scanner(System.in);
int n;
inst s=0;
System.out.println(“Enter a number : “);
n=in.nextInt();
if(n>0){
System.out.print(“Divisor of “+n+”;”);
for(int i=1;i<=n;i++){
if(n%i==0){
s+=i;
System.out.print(i+” “);}}
Sytem.out.println();
if(s<2*n)
System.out.println(n+” is a DEFICIENT NUMBER.”);
else if(s==2*n)
System.out.println(n+” is a PERFECT NUMBER.”);
else
System.out.println(n+” is an ABUNDANT NUMBER.”);
else
System.out.println(“Enter positive number only.”);}}

RESULT: The above java program successfully executed.


PROGRAM: 4
AIM:
To write a program in java to calculate a binary number.
ALGORITHM:
1) Start the program.
2) Import the package

importjava.util.Scanner;

3) Delcare the class, variables and member function.

public class BinNumber


public long BinAdd(long A,LongB)
4) Declare an array variable.

intR[]=new int[16];

5) Add the number

int R[] =new int[16];


R[k]=0;
int b1,b2,c,carry=0,i=15;
b1=(int)(a%10);
a/=10;
b2=(int)(b%10);
b/=10;
c=b1+b2+carry;
if(c==2){
c=0;
carry=1;}
else if(c==3){
c=1;
carry=1;}
else if(c==0||c==1)
carry=0;
else
return-1;
R[i--]=c;}
if(a>0){
b2=0;
b1=(int)(a%10);
a/=10;
c=b1+b2+carry;
if(c==2){
c=0;
carry=1;}
else if(c==3){
c=1;
carry=1;}
else if(c==0||c==1)
carry=0;
else
return-1;
R[i--]=c;}}
else if(b>0){
b1=0;
while(b>0){
b2=(int)(b%10);
b/=10;
c=b1+b2+carry;
if(c==2){
c=0;
carry=1;}
else if(c==3){
c=1;
carry=1;}
else if(c==3){
c=1;
carry=1;}
else if(c==0||c==1)
carry=0;
else
return-1;
R[i--]=c;}}
R[i--]=carry;
long sum=0;int d;

6) Create the scanner class.

Scanner in =new Scanner(System.in);


7) Print the number.

System.out.println(“The sum is :”+binNum3);


8) End the program
CODING:
importjava.util.Scanner;
public class BinNumbers
{
public long BinAdd(long A,long B)
{
int R[] =new int[16];
for(int k=0;k<8;k++)
R[k]=0;
int b1,b2,c,carry=0,i=15;
long a=1A,b=B;
while(a>0&&b>0)
{
b1=(int)(a%10);
a/=10;
b2=(int)(b%10);
b/=10;
c=b1+b2+carry;
if(c==2)
{
c=0;
carry=1;
}
else if(c==3)
{
c=1;
carry=1;
}
else if(c==0||c==1)
carry=0;
else
return-1;
R[i--]=c;
}
if(a>0)
{
b2=0;
while(a>0)
{
b1=(int)(a%10);
a/=10;
c=b1+b2+carry;
if(c==2)
{
c=0;
carry=1;
}
else if(c==3)
{
c=1;
carry=1;
}
else if(c==0||c==1)
carry=0;
else
return-1;
R[i--]=c;
}
}
else if(b>0)
{
b1=0;
while(b>0)
{
b2=(int)(b%10);
b/=10;
c=b1+b2+carry;
if(c==2)
{
c=0;
carry=1;
}
else if(c==3)
{
c=1;
carry=1;
}
else if(c==3)
{
c=1;
carry=1;
}
else if(c==0||c==1)
carry=0;
else
return-1;
R[i--]=c;
}
}
R[i--]=carry;
long sum=0;int d;
for(int j=0;j<16;j++)
{
sum=sum*10+R[j];
}
return sum;
}
public static void main(String[] args)
{
Scanner in =new Scanner(System.in);
long binNum1,binNum2,binNum3;
System.out.println(“Enter 1st binary number :”);
binNum1=in.nextLong();
System.out.println(“Enter 2nd binary number :”);
binNum2=in.nextLong();
BinNumbersbns = new BinNumbers();
binNum3=bns.BinAdd(binNum1,binNum2);
System.out.println(“The sum is :”+binNum3);
}
}

RESULT:
The above java program successfully executed.
PROGRAM: 5
AIM:
To write a program in java to print an 2 D array in spiral order.
ALGORITHM:
1) Start the program.
2) import the package.

Import java.util.Scanner;

3) Declare the class, variables and member function.

class Spiral
int[] []a;
int N;
4) Receive the array variable.

a=new int [n][n];


for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
a[i][j]=1+N*i+j;

5) Print the normal no.

sp.printNormal();

6) Print the spiral no.

sp.printSpiral();

7) End the program.


CODING:
import java.util.Scanner;
class Spiral
{
int[] []a;
int N;
Spiral (int n)
{
a=new int [n][n];
N=n;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
a[i][j]=1+N*i+j;
}
voidprintNormal()
{
for(int i=0;i<N;i++)
for(j=0;j<N;j++)
System.out.println(a[i][j]+”\t”);
}
}
voidprintSpiral()
{
for(int i=N-1,j=0;i>0;i-,j++)
{
for(int k=j;k<i;k++)
System.out.println(a[j][k]+” ”);
for(int k=i;k<j;k-)
System.out.println(a[k][i]+” “);
for(int k=i;k>j;k-)
System.out.println(a[k][j]+” “);
}
if(N%2==1)
System.out.println(a[(N-1)/2][(N-1)/2]);
}
public static void main(String [] args)
{
Scanner in =new Scanner(System.in);
System.out.println(“Enter N to create an N*N array is :”);
int N=in.nextInt();
Spiral sp=new Spiral(N);
System.out.println(“The given array is :”);
sp.printNormal();
System.out.println(“When printed in spiral order:”);
sp.printSpiral();
}
}

RESULT: The above java program successfully executed.

PROGRAM: 6
AIM
To accept the value of N and creates N*N Boolean array.
ALGORITHM:
1) Start the program.
2) Import the package.

import java.util.Scanner;

3) Declare the class, variables and member function.

Boolean a[ ][ ];
RelPrime()

4) Declare the array size.

a=new Boolean [100][100];

5) Create an 2*2 dimensional array.

void create(int n)
{
for(inti=0;i<n;i++)
{for(int j=0;j<n;j++)
{if(gcd(i,j)==1)
a[i][j]=true;
}}}

6) Display the number.

System.out.println(a[i][j]+”\t”);
System.out.println();}}
7) End the program.

CODING:
import java.util.Scanner;
public class RelPrime
{
boolean a[ ][ ];
RelPrime()
{
a=new Boolean [100] [100];
}
void create(int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(gcd(i,j)==1)
a[i][j]=true;
}}}
int gcd(int a, int b)
{
int num,den,GCD=0,r;
if(a<b)
{
num=a;
den=b;
}
else {
num =b;
den=a;
}
while(den>1)
{
r=num%den;
if(r==0)
{
GCD=den;
break;
}
else
{
num=den;
den=r;
}
}
if(den==1)
GCD=1;
return GCD;
}
void display(int n) {
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
Sstem.out.println(a[i][j]+”\t”);
System.out.println( );
}}
public static void main(String [ ] args)
{
Scanner in =new Scanner(System.in);
int n;
System.out.println(“Enter the value of n(<100):”);
n=in.nextInt( );
RelPrimerp=new RelPrime( );
rp.create(n);
rp.display(n);
}}

RESULT: The above java program successfully executed.


PROGRAM: 7
AIM:
To generate the Fibonacci strings.
ALGORITHM:
1) Start the program.
2) import the package.

import java.util.Scanner;

3) Declare the class, variables and member function.

public class Fibostring


{
String x,y,z;
int n;
publicFiboString( )

4) To accept the value.

void accept( )

5) To generate the no.

System.out.println(“ “+x+”,”+y);
for(int i=2;i<=n;i++) {
System.out.print(“,”+z);
x=y;
y=z;
z=y.concat(x);
6) End the program.
CODING:
importjava.util.Scanner;
public class Fibostring
{
String x,y,z;
int n;
public FiboString( )
{
x=”a”;
y=”b”;
z=”ba”;
}
void accept( )
{
Scanner sc=new Scanner(System.in);
System.out.println(“How many terms in Fibo String?”);
n=sc.nextInt( );
}
void generate( )
{
System.out.println(“ “+x+,”+y);
for(int i=2;i<=n;i++)
{
System.out.print(“,”+z);
x=y;
y=z;
z=y.concat(x);
}
}
public static void main( )
{
FiboStringfstr=new FiboString( );
fst.accept( );
fstr.generate( );
}
}

RESULT: The above java program successfully executed.


PROGRAM: 8
AIM:
To write a java program to print employee details and calculates the
employee’s Provident Fund and Gratuity.
ALGORITHM:
1) Start the program
2) Import the header file.

import java.io.*;

3) Declare the class, object and main function.

Class personal

4) Declare the variables.

String Name;
int pan;
double basic-pay;
int acc-no;

5) Declare the parameterized constructor to assign the value.


6) Display the employee details.
7) Declare another class with extends.

class Retire extends Personal

8) Declare the variables and main function.

int yrs;
double pf;
double grat;

9) Declare the parameterized constructor to assign the value of both the


class.
10) Declare the two functions to calculate the pf and gratuity of
employee.
11) Display the employee details with pf and gratuity amount.
12) End the program.
CODING:
class Personal
{
String Name;
int Pan;
double basic-pay;
int acc-no;
Personal(String nm1,intpa,doublebas,int ac)
{
Name=nm;
Pan=Pa;
Basic-pay=bas;
Acc-no=ac;
}
void display()
{
System.out.println(“Name=”+nm);
System.out.println(“Pan=”+Pa);
System.out.println(“Basic-pay=”+bas);
System.out.println(“acc-no=”+ac);
}
}
public class Retire extends Personal
{
int Yrs;
double Pf;
double break;
Retire(String nm,intPa,doublebas,intac,intyr)
{
super(nm,Pa,bas,ac)
Yrs=y;
void provided()
{
Pf=(2/100*bas)*Y;
}
void gratuity()
{
if(Y>=10)
{
Basic-pay*12;
}
else
{
Grat=0;
}
void display()
{
Super.display();
System.out.println(“Yrs=”+Y);
System.out.println(“Pf=”+Pf);
System.out.println(“grat=”+Grat);
}
}

Result: The above java program successfully executed.


PROGRAM: 9
AIM:
To write a java program insert a value from front end and rear end.
ALGORITHM:

CODING:

import java.io.*;
class circular
{
int arr[],lim,front,rear;
public circular()
{
arr=new int[100];
front=rear=0;
}
public circular(int 1)
{
lim=1;
front=rear=0;
arr=new int[1];
}
void addrear(int val)
{
if(rear<lim-1)
{
rear++;
arr[rear]=val;
}
else
{
System.out.println(“Overflow from rear”);
}
}
void addfront(int val)
{
{
if(front==0||front==1)
{
System.out.println(“Overflow from front”);
}
else
{
front--;
arr[front]=val;
}
}
int popfront();
{
if(front<=rear)
{
if(front==0)
{
front++;
}
int t=arr[front];
front++;
return(t);
}
else
{
return(-9999);
}
}
int poprear()
{
if(front>rear||front==0||rear==0)
{
return(-9999);
}
else
{
int t=arr[rear];

rear--;
return(t);
}
}
}

Result: The above java program successfully executed.


PROGRAM: 10
AIM:
To write the java program to insert a node in a sorted linked list.
ALGORITHM:
1) Start the program.
2) Import the header file.

import java.io.*;

3) Declare the class, object and main function.

Class node

4) Declare the variables.

int data;

5) Ptr=start
6) NEWPTR=new Node
7) if NEWPTR=NULL
8) else
9) NEWPTR.INFO=ITEM
10) NEWPTR.LINK=NULL
11) IF START=NULL then
12) START=NEWPTR
13) else if ITEM<START.INFO then
14) Save=start
15) START=NEWPTR
16) NEWPTR.LINK=Save
17) Save=Start
18) Repeat steps 16 through 22 until ptr=NULL
19) if NEWPTR.INFO<ptr.INFO then
20) Save=ptr
21) Ptr=ptr.LINK
22) else
23) Save.LINK=NEWPTR
24) NEWPTR.LINK=ptr
25) Break
26) if ptr=NULL then
27) Save.LINK=NEWPTR
28) NEWPTR.LINK=NULL
29) NEWPTR.LINK=NULL
30) End the program.
CODING:
import java.io.*;
class Node
{
protectedint data;
protected Node link;
public Node()
{
link=null;
data=0;
}
public Node(intd,Node n)
{
data=d;
link=n;
}
publicvoid setlink(Node n)
{
link=n;
}
publicvoid setData(int d)
{
data=d;
}
publicNode getlink()
{
return link;
}
publicintgetData()
{
return data;
}
}
classlinkedList
{
protectedNode start;
publiclinkedlist()
{
start=null;
}
publicbooleanisEmpty()
{
return start==null;
}
publicvoid Insert(intval)
{
Node nptr,ptr,save=null;
Nptr=new Node(val,null);
Boolean ins=false;
if(start==null)
{
start=nptr;
elseif(val<=start.getData())
nptr.setlink(start);
start=nptr;
}
else
{
save=start;
ptr=start.getlink();
while(ptr!=null)
{
if(val>=save.getData()&&val<=ptr.getData())
{
save.setlink(nptr);
nptr.setlink(ptr);
ins=true;
break;
}
else
{
save=ptr;
ptr=ptr.getlink();
}
}
if(ins==false)
{
save.setlink(nptr);
}
}
}
public void display()
{
Node ptr=start;
System.out.print(start.getData()+”-->”);
ptr=start.getlink();
while(ptr.getlink()!=null)
{
System.out.print(ptr.getData()=”-->”);
ptr=ptr.getlink();
}
System.out.print(ptr.getData()+”!!!!”);
System.out.println();
}
}
classlinListTest
{
protected static linkedList S;
public static void main(String[]args)
{
intnum;
S=new linkedList();
BufferedReaderbr=new BufferedReader(new
InputStreamReader(System.in));
System.out.println(“……Starting List Test for INSERTION……\n”);
for(int a=0;a<5;++a)
{
System.out.print(“Enter a number :”);
try
{
num=Integer.parseInt(br.readLine());
S.Insert(num);
System.out.println(“Inserted : “+num);
}
catch(Exception e)
{
System.out.println(e);
}
}
System.out.println(“\n created List is :”);
S.display();
System.out.println(“\n---List test over---“);
}
}

Result: The above java program successfully executed.


PROGRAM: 11
AIM:
Write a java program to generate palindrome prime numbers using method.
ALGORITHM:
1) Start the program
2) Import the header file

importjava.util.Scanner;

3) Declare the class object

public class PalPrime

4) Declare the variables

intmid,n;

5) Check number is prime or not

int mid=n/2;
for(int i=2;i<=mid;i++)
if(n%i==0)
return false;

return true;

6) Check number is palindrome or not

int n1,rev=0,r;
n1=n;
while(n1!=0)
r=n1%10;
rev=rev*10+r;
n1=n1/10;
if(rev==n)
return true;
else
return false
7) Generate palprime

public static void GenPalPrime(int n)


int count=0;
if(n==2)
System.out.println(“11”);
count++;
else if(n==3)
int n1=101,nn;
for(int n2=2;n2<=9;n2++)
if(IsPrime(n2)==true)
nn=n1+n2*10;
if(IsPrime(nn)==true&&IsPal(nn)==true)
System.out.println(nn);
count++;

8) Declare the main function


9) End the program.
CODING:
importjava.util.Scanner;
public class PalPrime{
public static booleanIsPrime(int n){
int mid=n/2;
for(inti=2;i<=mid;i++)
{
if(n%i==0)
return false;
}
return true;
}
public static booleanIsPal(int n)
{
int n1,rev=0,r;
n1=n;
while(n!!=0)
{
r=n1%10;
rev=rev*10+r;
n1=n1/10;
}
if(rev==n)
return true;
else
return false;
}
public static void GenPalPrime(int n)
{
omt cpimt=0;
if(n==2)
{
System.out.println(“11”);
count++;
}
else if(n==3)
{
int n1=101,nn;
for(int n2=2;n2<=9;n2++)
if(IsPrime(n2)==true)
{
nn=n1+n2*10;
if(IsPrime(nn)==true&&IsPal(nn)==true)
{
System.out.println(nn);
count++;
}
}
}
else if(n==4)
{
int n1=1001,nn;
for(int n2=10;n2<=99;n2++)
if(IsPrime(n2)==true)
{
nn=n1+n2*10;
if(IsPrime(nn)==true&&IsPal(nn)==true)
{
System.out.println(nn);
count++;
}
}
}
else if(n==5)
{
int n1=10001,nn;
for(int n2=100;n2<=999;n2++)
if(IsPrime(n2)==true)
{
nn=n1+n2*10;
if(IsPrime(nn)==true&&IsPal(nn)==true)
{
System.out.println(nn);
count++;
}}}
else
System.out.println(“Value of n should be within range 2…5”);
if(n>=&&n<=5&&count==0)
System.out.println(“No PalPrime number of this width”);
}
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println(“Enter the width of palprime numbers(i.e., no. of digits in a
palprime no.)”);
int n=in.nextInt();
GenPalPrime(n);
}
}

Result: The above java program successfully executed.


PROGRAM: 12
AIM:
Write a java program to find the Brun’s Constant by using twin prime
numbers.
ALGORITHM:
1) Start the program.
2) Import the header file

importjava.util.Scanner;

3) Declare the class object

public class Primes

4) Declare the variables

intmid,n;

5) To check the number is prime or not

int mid=n/2;
for(int i=2;i<=mid;i++)
if(n%i==0)
return false;
return true;

6) To check the number is twin prime or not

for(inti=3;i<=lim;i+=2)
if(IsPrime(i)==true&&IsPrime(i+2)==true)
System.out.println(“(“+i+”,”+(i+2)+”)”);

7) To find the BrunConstant

double s=0;
for(inti=3;i<=lim;i+=2)
if(IsPrime(i)==true&&IsPrime(i+2)==true)
s+=(1/(float)(i)+1/(float)(i+2));
System.out.println(“+(1/”+i+”+1/”+(i+2)+”)”);
System.out.println();
return s;

8) Declare the main function


9) End the program

CODING:
import java.util.Scanner;
public class Primes
{
public booleanIsPrime(int n)
{
int mid=n/2;
for(inti=2;i<=mid;i++)
{
if(n%i==0)
return false;
}
return true;
}
public void twinPrimes(intlim)
{
for(inti=3;i<=lim;i+=2)
{
if(IsPrime(i)==true&&IsPrime(i+2)==true)
System.out.println(“(“+i+”,”+(i+2)+”)”);
}
}
public double BrunConstant(intlim)
{
double s=0;
for(inti=3;i<=lim;i+=2)
{
if(IsPrime(i)==true&&IsPrime(i+2)==true)
{
s+=(1/(float)(i)+1/(float)(i+2));
System.out.println(“+(1/”+i+”+1/”+(i+2)+”)”);
}
}
System.out.println();
return s;
}
public static void main(String[] args)
{
Prime pr=new Primes();
Scanner in=new Scanner(System.in);
int n=0;
System.out.print(“Enter limit: “);
n=in.nextInt();
doublebrun=pr.BrunConstant(n);
System.out.printl(“Brun’s constant below limit “ +n+ “ is: “ +brun);
}}

Result: The above java program successfully executed.


PROGRAM: 13
AIM:
Write a java program that displays the values of Sin x or Cos x or any other
trigonometric function depending upon user’s choice.
ALGORITHM:
1) Start the program
2) Import the java package

importjava.util.*;

3) Declare the class object and main function

public class Trignometry

4) Declare the variables

int n;

5) Find the factorial

long f=1;
for(inti=2;i<=n;i++)
f=f*i;
return f;

6) Value of sin x

public double Sin(double x)


inti;
x=x%(2.Math.PI);
double sum=0;
for(i=0;i<N;i++)
sum=sum+Math.pow(-1,i)*Math.pow(x,2*i+1)/Factorial(2*i+1);
return sum;

7) Value of cos x

public double Cos(double x)


x=x%(2.Math.PI);
inti;
double sum=1;
for(i=0;i<=N;i++)
sum=sum+Math.pow(-1,i)*Math.pow(x,2*i+1)/Factorial(2*i+1);
return sum;

8) Value of other trigonometrical function

x=x%(2*Math.PI);
doublesinx,cos,,tanx=0,cotx=0,cosecx=0,secx=0;
sinx=Sin(x);
cosx=Cos(x);
if(sinx !=0)
cotx=cosx/sinx;
cosecx=1/sinx;
if(cosx !=0)
tanx=sinx/cosx;
secx=1/cosx;
if(sinx !=0&&cosx !=0)
System.out.println(“Tan x=” + tanx +”,Cot x=” + cotx +”,Cosecx=”
+ cosecx +”,Secx=” + secx);
else if(sinx==0)
System.out.println(“Tan x=” + tanx +”Cotx=infinite,Cosec
x=infinite,Secx=” + secx);
else if(cosx==0)
System.out.println(“Tan x=infinite,Cot x=” + cotx +”,Cosec x=” +
cosecx +”,Sec x=infinite”);

9) Declare the main function


10) End the program
CODING:
importjava.util.Scanner;
public class Trignometry
{
int N:
Trignometry(int n){N=n;}
private long Factorial(int n)
{
long f=1;
for(inti=2;i<=n;i++)
{
f=f*i;
}
return f;
}
public double Sin(double x)
{
inti;
x=x%(2*Math.PI);
double sum=0;
for(i=0;i<N;i++)
{
sum=sum + Math.pow(-1,i)*Math.pow(x,2*i+1)/Factorial(2*i+1);
}
return sum;
}
public double Cos(double x)
{
x=x%(2*Math.PI);
inti;
double sum=1;
for(i=0;i<=N;i++)
{
sum=sum + Math.pow(-1,i)*Math.pow(x,2*i+1)/Factorial(2*i+1);
}
return sum;
}
public void Others(double x)
{
x=x%(2*Math.PI);
doublesinx,cosx,tanx=0,cotx=0,cosecx=0,secx=0;
sinx=Sin(x);
cosx=Cos(x);
if(sinx !=0)
{
cotx=cosx/sinx;
cosecx=1/sinx;
}
if(cosx !=0)
{
tanx=sinx/cosx;
secx=1/cosx;
}
if(sinx !=0&&cosx !=0)
System.out.println(“Tan x=” + tanx +”,Cot x=” + cotx +”,Cosecx=” + cosecx
+”,Secx=” + secx);
else if(sinx==0)
System.out.println(“Tan x=” + tanx +”Cotx=infinite,Cosec x=infinite,Secx=” +
secx);
else if(cosx==0)
System.out.println(“Tan x=infinite,Cot x=” + cotx +”,Cosec x=” + cosecx +”,Sec
x=infinite”);
}
public static void main(Strin[] args)
{
Scanner in=new Scanner(System.in);
int n;
System.out.println(“Specify no. of terms for the cos and sin series :”);
n=in.nextInt();
double x;
System.out.print(“Enter the value of angle x :”);
x=in.nextDouble();
Trignometryang=new Trignometry(n);
int choice;
do
{
System.out.println(“Menu”);
System.out.println(“1. Sin x”);
System.out.println(“2. Cos x”);
System.out.println(“3. Other angles “);
System.out.println(“4. Exit”);
System.out.println(“Enter your choice(1-4): “);
choice=in.nextInt();
doubleval=0;
switch(choice)
{
case 1: val=ang.Sin(x);
System.out.println(“Sin(“ +x+ “): “+val);
break;
case 2: val=ang.Cos(x);
System.out.println(“Cos(“ +x+ “): “+val);
break;
case 3: ang.Others(x);
break;
case 4: break;
default: System.out.println(“Wrong Choice! Should be 1-4”);
break;
}}
while(choice>=1 && choice <4);}}

Result: The above java program successfully executed.


PROGRAM: 14

AIM:
To write a java program to represent a line and a sub class Circle to find the
length of radius and the area of circle by using the required data members of
super class.

ALGORITHM:
1) Start the program.
2) Import the header file.

Import java.io.*

3) Declare the class, object and main function

Class plane

4) Declare the variables

int x1,y1;

5) Declare the parametrised constructor to assign the value.

X1=nx;
Y1=ny;

6) Display the coordinates.


7) Declare the another class with extends

Class circle extends plane

8) Declare the variables.

int x2,y2;
doubleradius,area;

9) Declare the parametrised constructor to assing the values of both the


class.
10) Declare the two function to calculate the radius and area of the
circle.
11) End the program.
CODING:

import java.io.*;
Public class Plane
{
int x1,y1;
public Plane(intnx,intny)
{
void Show()
{
System.out.println(“(“ +x1+ “,” +y1+ “)”);
}
}
public class Circle extends Plane
{
int x2,y2;
doubleradius,area;
public Circle(intnxt,int ny1,int nx2,int ny2)
{
super(nx1,ny1);
x2=nx2;
y2=ny2;
}
voidfindRadius()
{
intdiffx,diffy;
diffx=x2-x1;
diffy=y2-y1;
radius=Math.sqrt(diffx*diffx=diffy*diffy);
}
voidfindArea()
{
area=3.14159*radius*radius;
}
void Show()
{
System.out.println(“Pt1=(“+x1+”,”+y1+”)\tPt2=(“+x2+”,”+y2+”)”);
System.out.println(“Radius=”+radius);
System.out.println(“Area=”+area);
}}
Result: The above java program successfully executed.
PROGRAM: 15
AIM:
To write a java program to store the names and rank of 50 students. Define a
sub-class to find the highest rank along with the name.
ALGORITHM:
1) Start the program.
2) Import the header file

Import java.util.Scanner

3) Declare the class, object and main functions.

Class Record

4) Declare the variables

String [] names;
int [] rnk;

5) Constructor to initialize the data members.

names=new String[5];
rnk= new int[5];

6) Display the name and rank of the students.


7) Declare new class with extends.

Class Rank extends Record

8) Declare the variables.

int index;

9) Declare the constructor to invoke base class.

index=0;

10) Declare the another function to find index/location of the topmost


rank and stores it in index.
11) Display the name and rank of the topmost rank.
12) End the program
CODING:

importjava.util.Scanner;
public class Record
{
String[]names;
int[] rnk;
Record()
{
Names=new String[5];
}
voidreadvalues()
{
Scanner kb=new Scanner(system.in);
for(inti=0;i<5;i++)
{
System.out.println(“Name”+names[i]+”Rank”+rnk[i]);
}
}
}
class Rank extends Record
{
int index;
rank()
{
super();
index=0;
}
voidhighest()
{
int max=rank[0];
index=0;
for(inti=1;i<5;i++)
{
if(max<rnk[i])
{
index=1;
}}}
void display()
{
Super.display();
System.out.println(“Name of topmoster ranker is “+names[index]);
}
}

Result: The above java program successfully executed.

PROGRAM: 16
AIM:
To write a java program to store the details of the stock of a retail store. Define
a subclass to store the details of the items purchased with the new rate and
update the stock.
ALGORITHM:
1) Start the program.
2) Import the header file.

Import java.io.*;

3) Declare the class, object and main function.

class stock

4) Declare the variables

String item;
intqty,rate,amt;

5) Declare the parametrised constructor to assign the values.


6) Display the stock details.
7) Delcare the new class with extends.

Class purchase extends stock

8) Declare the variables

intpqty,prate;

9) Declare the parametrised constructor to assign the values of both the


classes.
10) Declare another function to calculate the current stock value.

amt=qty*rate;

11) Display the stock details.


12) End the program.

CODING:
import java.io.*;
public class stock
{
String item;
doubleqty,rate,amt;
public Stock(String a,doubleb,double c)
{
item=a;
qty=b;
amt=qty*rate;
}
void display()
{
System.out.println(“Item Name:”+item);
System.out.println(“Quantity:”+qty);
System.out.println(“Rate(per unit):”+rate);
System.out.println(“Net Value:”+amt);
}
}
class Purchase extends Stock
{
intpqty;
double prate;

public Purchase(String a,doubleb,doublec,int a1,double b1)


{
super(a,b,c);
pqty=a1;
prate=b1;
}
void update()
{

qty +=pqty;
if(prate !=rate)
rate=prate;
amt==qty*rate;
}
void display();
update();
super.display();
}
}

RESULT: The above java program successfully executed.

PROGRAM 17:
AIM:
To write a java program to store the details of a worker. Define a sub class to
compute the monthly wages for the worker.

ALGORITHM:
1. Start the program.
2. Import the header file.

import java.io.*;

3. Declare the class ,object and main function.

class worker

4. Declare the variables.

String name;
double basic;

5. Declare the parametrised constructor to assign the values.


6.Display the worker details.
7.Declare the another class extends.

Class wages extends worker

8. Declare the variables.

Inthours, rate ,wage;


9 Declare the parametrised constructor to assign the values.
10. Declare the another constructor to return the overtime amount.

Return hours*rate;

11.Calculate the wage.

wage =overtime()+basic;

12.Display the wage and other details of the worker. 13.End the program.

CODING:
import java.io.*;
class worker
{
String Name;
double Basic;
Worker(String nm,doublebas)
{
Name=nm;
Basic=bas;
}
void display()
{
System.out.println(“NAME:”+Name); System.out.println((“BASIC;”+Basic); }
}
}
public class Wages extends Worker
{
inthrs;
double rate;
double wages;
Wages(string nm,doublebas,inthr,doublert)
{
super(nm,bas);
Hrs=hr;
Rate=rt;
}
double overtime()
{
Return hrs*rate;
}
void display()
{
Super.display();
Wage=overtime()+Basic;
System.out.println(“Hours worked:”+hrs); System.out.println(“Rate:”+rate);
System.out.println(“Wage:”+Wage);
}
}

RESULT: The above java program successfully executed.

PROGRAM 18:
AIM:
To write a program in java to calculate the binary addition.
ALGORITHM :
1. Start the program.
2. Import the package

import java.util.Scanner;

3 Declare the class, variables and member function.

public class BinNumber


public long BinAdd(long A,longB)

4)-Declare an array variable.

intR[]=new int[16];

5)-Add the number

R[k]=0;
int b1 ,b2,c,carry=0,i=15;
b1=(int)(a%10);
a/=10;
b2=(int)(b°/10);
b/=10;
c=b1+b2+carry;
if(c==2){
c=0;
carry=1;}
else if(c==3){
c=1;
carry=1;}
else if(c==0||c==1)
carry=0;
else
return-1;
R[i--]=c;}
if(a>0){
b2=0;
b1=(int)(a%10);
a/=10;
c=b1+b2+carry;
if(c==2){
c=0;
carry=1;}
else if(c==3){
c=1 ;
carry=1 ;}
else if(c==0||c==1)
carry=0;
else
return-1;
R[i--]==c;}}
else if(b>0){
b1=0;
whi1e(b>0){
b2=(int)(b%10);
b/=10;
c=b1+b2+carry;
if(c==2){
c=0;
carry=1;}

else if(c==0||c==1)
carry=0; carry=1;}
else if(c==3){
c=1;
carry=1;}

else if(c==3){

c=1;
else

return-1;
R[i--]=c;}}
R[i--]=carry;
long sum==0;int d;
for(j=0;j<16;j++)
sum=sum*10+R[j];
return sum;

6) Create the scanner class.

Scanner in=new Scanner (System.in);

7) Print the sum


System.out.println("The sum is :"+binNum3)

8) End the program.

CODING:
import java.util.Scanner;
public class BinNumbers
{
public long BinAdd(long A,long B)
{
int R[ ] =new int[16];
for(int k=0;k<8;k++)
R[k]=0;
int b1,b2,c,carry==0,i=15;
long a=A,b=B;
while(a>0&&b>0)
{
b1=(int)(a%10);
a/=10;
b2=(int)(b%10);
b/=10;
c=b1+b2+carry;
if(c==2)
{
c=0;
carry=1;
}
else if(c==3)
{
c=1 ;
carry=1 ;
}
else if(c==0||c==1)
carry=0;
else
return-1;
R[i--]=c;
}
if(a>0)
{
b2=0;
while(a>0)
{
b1=(int)(a%10);
a/=10;
c=b1+b2+carry;
if(c==2)
{
c=0;
carry=1;
}
else if(c==3)
{
c=1;

carry=1;
}
else if(c==0||c==1)
carry=0;
else
return-1;
R[i--]=c;
}
}
else if(b>0)
{
b1=0;
while(b>0)
{
b2=(int)(b%10);
b/=10;
c=b1+b2+carry;
if(c==2)
{
c=0;
carry=1;
}
else if(c==3)
{
c=1;
carry=1; }
else if(c==3)
{
c=1;
carry=1;
}
else if(c=0||c=1)
carry=0;
else
return-1;
R[i--]=c;
}
}
R[i--]=carry;
long sum=0;int d;
for(int j=0;j<16;j++)
{
sum=sum*10+R[j];
}
return sum;
}
public static void main(String[] args)
{
Scanner in ==new Scanner(System.in);
long binNum1,binNum2,binNum3;
System.out.println("Enter 1st binary number :"); binNum1=in.nextLong();
System.out.println("Enter 2nd binary number: "); binNum2=in.nextLong();
BinNumbers bns = new BinNumbers();
binNum3=bns.BinAdd(binNum1,binNum2); System.out.println("The sum
is :"+binNum3);
}
}
Result: The above java program is successfully executed.
PROGRAM 19:
AIM:
Write a program in java to check whether a number is circular prime or not
ALGORITHM:
1)Start the program.
2)lmport the java package.

Import java.util.*;

3)Declare the class,object and main function.

Class CircularPrime

4)Declare and initialize the variables.

int n,num,digit=0,p,n1,c,k=0;

5)Accept the no. from user.

n=in.nextlnt();

6)Count the digits of the no.

7) num=n;
p=(int)Math.pow(10,digit-1);

8)Repeat steps 8 and 9 till digit becomes 0.

n1=(num%p)*10+(num/p);
9)Check the no. n1 whether It is prime or not. If the no. n1 is prime,then make
k=1,otherwise print that n is not a circular prime.

10) lf(k==1)
System.out.prlntln(n+ “IS A CIRCULAR PRIME”);

11)End the program.

CODING:
import java.util.*;
class CircularPrime
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n,num,digit=0,p,n1,c,k=0;
System.out.println("Enter the number:");
n=ln.nextlnt();
num=n;
while(num!=0)
{
digit++;
num/=10;
}
num=n;
p=(int)Math.pow(10,digit-1);
while(digit!=0)
{
n1=(num%p)*10+(num/p);
c=0;
for(int i=1;i<=n1;i++)
{
if(n1%i==0)
c++;
}
if(c==2)
{
k=1;
System.out.println(n1);
num=n1;
}
else
{
System.out.println(n+"IS NOT A CIRCULAR PRIME");
break;
}
digit--;
}
if(k==1)
System.out.println(n+"lS A CIRCULAR PRIME");
}
}

RESULT: The above java program successfully executed.


PROGRAM 20:
AIM:
Write a program in java to check whether a number is EVIL number or not.
ALGORITHM:
1)Start the program
2)lmport the java package.

import java.util.*;

3)Declare the class,object and main function.

public class EvilNumber

4)Declare and initialize the variables.

int n,num,r,c=0;
String s:” ”;
long bin;

5)Accept the number from the user.

n=in.nextlnt();

6)Calculate the binary equivalent of the no and print.

num=n;
while(num!=0){
r=num%2;
s=lnteger.toString(r)+s;
num/=2;}
7)Count the no. of 1’s in the binary equivalent of the no., store it in c and print.
8)Check the value of c.

if(c%2==0)
System.out.println(” EVIL number”);
else
System.out.println(“NOT AN EVIL number”);
9)End the program.

CODING:
import java.util.*;
class EvilNumber
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n,num,r,c=0;
String s=" “;
System.out.println(“ENTER THE NUMBER:");
n=in.next|nt();
num=n;
while(num !=0) {
r=num%2;
s=Integer.toString(r)+s;
num=num/2;
}
long bin=Long.parseLong(s);
System.out.println("BINARY EQUIVALENT:" +bin);
while(bin!=0)
{
r=(int)bin%10;
if(r==1)
c++;
bin=bin/10;
}
System.out.println("N0. OF 1's:" +c);
if(c%2==0)
System.out.println("EVIL NUMBER");
else
System.out.println(“NOT AN EVIL number”);
}
}

RESULT: The above java program is successfully executed.

PROGRAM 21:
AIM:
Write a program in java to generate the Kaprekar numbers within a range.
ALGORITHM:
1)Start the program.
2)Import the java package.

import java.util.*;

3)Declare the class and instance variables.

public class Kaprekar{


int c;

4)Declare a function public void generateKaprekar(int n) which display the


Kaprekar numbers

int digit=0,num=n,r,sum=0;
while(num!=0){
digit++;
num=num/10;}
num=n*n;
while(digit!=0){
r=num%10;
sum=sum*10+r;
num=num/10;
digit--;}
c=0;
if(num+reverse(sum)==n){
c++;
System.out.print(n +",");}

5)Declare a function public void display() which display the frequency of


Kaprekar numbers.

System.out.println("The frequency of Kaprekar numbers:" +c);

6)Declare the main function to create object of the class.

Kaprekar kp=new Kaprekar();

7)Accept the lower and upper range from the user.

int n1=kp.nextlnt();
int n2=kp.nextlnt();

8)Now call the functions accordingly to generate the Kaprekar no.s between n1
and n2.

for(int i=n1;i<=n2;i++)
kp.generateKaprekar(i);
kp.display();

9)End the program.

CODING:
import java.util.Scanner;
public class Kaprekar{
int c;
public void generateKaprekar(int n)
{
int digit=0,num=n,r,sum=0;
while(num!=0)
{
digit++;
num=num/10;
}
num=n*n;
while(digit!=0)
{
r=num%10;
sum=sum*10+r;
num=num/10;
digit--;
}
c=0;
if(num+reverse(sum)==n)
{
c++;
System.out.print(n +",");
}
}
public int reverse(int p)
{
int d,rev=0;
while(p!=0)
{
d=p%10;
rev=rev*10+d;
p=p/10;
}
return rev;
}
public void display()
{
System.out.println("The frequency of Kaprekar numbers:" +c);
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
Kaprekar kp=new Kaprekar();
int n1,n2;
System.out.println("Enter the lower range:"); n1=in.nextlnt();
System.out.println("Enter the upper range:");
n2=in.nextlnt();
System.out.println("The Kaprekar numbers are:"); for(int i=n1;i<n2;i++)
kp.generateKaprekar(i);
kp.display();
}
}

RESULT: The above java program is successfully executed.


PROGRAM: 22
AIM:
Write a program in java to display the palindrome words in a String.
ALGORITHM:
1)Start the program.
2)lmport the Java package.

import java.io.*;

3)Declare the class, object and main function.

public class Palindrome

4)Declare and initialize the variables.

String s,st=" ",rev= " ";


char c;
int i,j,l;

5)Accept the string from the user.

s=in.readLine();
6) s=s+ ” ”;

7)Find the length of the string.

l=s.length();

8)From i=0 to i<l repeat step 9 to step 15.


9)c=s.charAt(i);
10)lf(c!= ’ ’) goto step 11 otherwise goto step 12.

11)st=st+c;
12)From j=0 to j<st.length() repeat step 13.
13)rev=st.charAt(j)+rev;
14)if(st.equalslgnoreCase(rev)) print st+" ".

15)End the program.

CODING:
import java.io.*;
public class Palindrome
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
String s,st= ””,rev= “”;
char c;
int i,j,l;
System.out.println("Enter the string:");
s=in.readLine();
s=s+" ";
l=s.length();
System.out.println(”The Palindrome words are:");
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
st=st+c;
else
{
for(j=0;j<st.length();j++)
rev=st.charAt(j)+rev;
if(st.equalslgnoreCase(rev))
System.out.print(st+" ”);
st="";rev="";
}
}
}
}

RESULT: The above java program is successfully executed.

PROGRAM 23:
AIM:
Write a java program to find the frequency of each character of a string.
ALGORITHM:
1)Start the program.
2)lmport the java package.

import java.io.*;

3)Declare the class, object and main function.

public class Frequency

4)Declare and initialize the variables.

String str;
char c,ch;
int i,j,l,freq=0,f=-1;

5)Accept the string from user.

str=in.readLine( );

6) str=str.toUpperCase();
7)Find the length of the string.

l=str.length();

8)From i=0 to i<l repeat step 9 to step 17.


9)c=str.charAt(i);

10)lf(c!= ’ ‘) goto step 11.

11)From j=0 to j<l repeat step 12 and step 13.

12)ch=str.charAt(j);

13)if(c==ch)

freq=freq+1;

14)From k=0 to k<l repeat step 15.


15)If(c==str.charAt(k))
f=1;

16)lf(f==-1)

System.out.println(c+ " " +freq);

17)freq=0;f=-1;
18)End the program.

CODING:
import java.io.*;
public class Frequency
{
public static void main(String args[])throws IOException
{
lnputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
String str;
char c,ch;
int i,j,l,freq=0,f=-1;
System.out.prlntln(”Enter the string:”);
str=in.readLine();
str=str.toUpperCase();
l=str.length();
System.out.prlntln(”Character“ +" "+ "Frequency");
for(i=0;i<l;i++)
{
c=str.charAt(i);
if(c!=' ')
{
for(j=0;j<l;j++)
{
ch=str.charAt(j);
if(c==ch)
freq=freq+1;
}
for(int k=0;k<i;k++)
{
if(c==str.charAt(k))
f=1;
}
if(f==-1)
System.out.println(c +" "+ freq);
}
freq=0;f=-1;
}
}
}

RESULT: The above java program is successfully executed.


PROGRAM 24:
AIM:
Write a java program to print the sentence in Iexographical order of Words.

ALGORITHM:
1)Start the program.
2)lmport the java package

import java.io.*;

3)Declare the class, object and main function.

public class Lexographical

4)Declare and initialize the variables.

String s,st= "",temp="" ,str= " ";


char c;
int i,j,l,k=0;
String wrd[ ]=new String[50];

5)Accept the string from user.

s=in.readLine( );

6)s=s+ ” ”;

7)Find the length of the string.


l=str.length();

8)From i=0 to i<l repeat step 9 to step 12.

9)c=str.charAt(i);

10)lf(c!= ’ ‘) goto step 11 otherwise goto step 12.

11)st=st+c;

12)wrd[k++]=st;
st= "";
13)From i=0 to i<k-1 repeat step 14 to step 16.

14)From j=0 to j<(k-1)-i repeat step 15 to step 16.


15)lf((wrd[j].compareTo(wrd[j+1]))>0) goto step 16.
16)temp=wrd[j];wrd[j]=wrd[j+1];wrd[j+1]=temp;

17)From i=0 to i<k repeat step 18.

18)str=wrd[i]+ ” ”;

19)Print str.

20)End the program.

CODING:
import java.io.*;
public class Lexographical
{
public static void main(String args[])throws lOException
{
InputStreamReader read=new lnputStreamreader(System.in);
BufferedReader in=new BufferedReader(read);
String s,st="",temp="",str=" ";
char c;
int i,j,l,k=0;
String wrd[]=new String[50];
System.out.println("Enter the string");
s=in.readLine();
s=s+" ";
l=s.length();
for(i=0;i<l;i++){
c=s.charAt(i);
if(c!=' ')
st=st+c;
else{
wrd[k++]=st;
st="";}}
for(i=0;i<k-1;i++){
for(j=0;j<(k-1)-i;j++){
if((wrd[j].compareTo(wrd[j+1]))>0){
temp=wrd[j];
wrd [j]=wrd [j+1];
wrd[j+1]=temp;
}}}
System.out.println("The sentence in lexographical order is:");
for(i=0;i<k;i++)
str=wrd[i]+" ";
System.out.println(str);
}
}

RESULT: The above java program is successfully executed.

PROGRAM 25:
AIM:
Write a java program to find the frequency of consecutive letters in each word
of a string and dlsplay the word having maximum frequency of consecutive
letters.
ALGORITHM:
1)Start the program.
2) import the java package.

import java.io.*;

3)Declare the class, object and main function.

public class Consecutive

4)Declare and initialize the variables.

String s,st= ””,max_Word= “ ”;


char c;
int i,j,l,max=0,f;

5)Accept the string from user.

s=in.readLine( );

6) s=s.toUpperCase();s=s+ “ ”;

7)Find the length of the string

l=s.length();
8)From i=0 to i<I repeat step 9 to step 17.
9)c=str.charAt(i);
10)lf(c!=' ‘) goto step 11 otherwise goto step 12.
11)st=st+c;
12)f=0;
13)From j=0 to j<st.|ength( )-1 repeat step 14.
14)if(st.charAt(j)==st.charAt(j+1))
f=f+1;
15)System.out.println(st+” "+f);
16)if(max<f){
max=f;
max_Word=st;}
17)f=0;st= "”;
18)Display max_Word and max.
19)End the program.

CODING:
import java.io.*;
public class Consecutive
{
public static void main(String args[])throws lOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
String s,st="",maX_Word="";
char c;
int i,j,l,max=0,f;
System.out.println("Enter the string:");
s=in.readLine();
s=s.toUpperCase();
s=s+" ";
l=s.length();
System.out.println("Word" +" "+ ”Consecutive Letter Frequency");
for(i=0;i<l;i++){
c=s.charAt(i);
if(c!=' ')
st=st+c;
else{
f=0;
for(j=0;j<st.length()-1;j++){
if(st.charAt(j)==st.charAt(j+1))
f=f+1;}
System.out.println(st +" "+ f);
if(max<f){
max=f;
max_Word=st;}
f=0;st="";}}
System.out.println("The word having maximum frequency of consecutive Ietter
is" +max_Word + "and the consecutive letter frequency in it is" +max);
}}

RESULT: The above java program is successfully executed.

PROGRAM: 26
AIM:
Write a program in java to search an element in an array using LinearSearch.
ALGORITHM:
1)Start the program.
2)import the java package.

import java.util.*;

3)Declare the class, object and main function.

public class LinearSearch

4)Declare and initialize the variables.

int n[ ]=new int[6];


int i,ns,k=-1;

5)Accept the array elements and no. to be searched from the user.

for(i=0;i<6;i++)
n[i]=in.nextlnt();

6)From i=0 to i<6 repeat this step.

if(ns==n[i]){
k=i;break;}

7) if(k!=-1)
System.out.println("The number present at position” +(k+1));
else
System.out.println("The number is not present");

8)End the program.

CODING:
import java.util.Scanner;
class LinearSearch{
public static void main(String args[])
{Scanner in=new Scanner(System.in);
int n[]=new int[6];
int i,ns,k=-1;
System.out.println("Enter the array elements:"); for(i=0;i<6;i++)
n[i]=in.nextlnt();
System.out.println("Enter the number to be searched:");
ns=in.nextlnt();
for(i=0;i<6;i++)
{if(n[i]==ns)
{k=i;
break;}}
if(k!=-1)
System.out.println("The number present at position" +(k+1));
else
System.out.println("The number is not present.");
}
}
RESULT: The above java program is successfully executed.
PROGRAM: 27
AIM:
Write a program in java to search an element in an array using Binarysearch.
ALGORITHM:
1)Start the program.
2)lmport the java package.

import java.util.Scanner;

3)Declare the class,object and main function.

class BinarySearch

4)Declare and initialize the variables.

int arr[ ]=new int[6];


int ns,lb=0,ub=5;mid,i,k=-1;

5)Accept the array elements and no. to be searched from the user.

for(i=0;i<6;i++)
arr[i]=in.nextlnt();
ns=in.nextlnt();

6)Until (lb<=ub) repeat step 7 to step.


7)mid=(lb+ub)/2;

if(ns==arr[mid]){
k=mid;break;}
else goto step8.
8) else if(ns<arr[mid])

ub=mid-1;
else goto step9.

9) lb=mid+1;
10) if(k!=-1)
System.out.println("The number present at position” +(k+1));
else
System.out.println("The number is not present."); ,

11)End the program.

CODING:
import java.util.Scanner;
class BinarySearch
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int arr[]=new int[6];
int i,ns,lb,ub,mid,k;
k=-1;lb=0;ub=5;
System.out.println(”Enter the array elements:"); for(i=0;i<6;i++)
{
arr[i]=in.nextlnt();
}
System.out.println("Enter the number to be searched:");
ns=in.nextlnt();
while(lb<=ub)
{
mid=(lb+ub)/2;
if(arr[mid]==ns)
{
k=mid;
break;
}
else if(arr[mid]<ns)
lb=mid+1;
else
ub=mid-1;
}
if(k!=-1)
System.out.println("The number present at position" +(k+1));
else
System.out.println("The number is not present.");
}
}

RESULT: The above java program is successfully executed.

PROGRAM 28:
AIM:
Write a program in java to arrange the numbers in the array in ascending order
using SelectionSort technique.
ALGORITHM:
1)Start the program.
2)lmport the java package.

import java.util.Scanner;

3)Declare the class,object and main function.

class SelectionSort

4)Declare and initialize the variables.

int n[ ]=new int[6];


int i,j,min,temp;

5)Accept the array elements.

for(i=0;i<6;i++)
n[i]=in.nextlnt();

6)From i=0 to i<5 repeat step 7 to step 10.

7)min=i;

8)From j=i+1 to j<6 repeat step 9.


9)lf(n[j]<n[min]) then min=j.
10)temp=n[i];n[i]=n[min];n[min]=temp;

11)Print the sorted array.


12)End the program.

CODING:
import java.util.Scanner:
class SelectionSort{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int n[]=new int[6];
int i,j,min,temp;
System.out.println("Enter the array elements:"); for(i=0;i<6;i++)
n[i]=in.nextlnt();
for(i=0;i<5;i++){
min=i;
for(j=i+1;j<6;j++){
if(n[j]<n[min])
min=j;}
temp=n[i];
n[i]=n[min];
n[min]=temp;}
System.out.println("The numbers in ascending order are:");
for(i=0;i<6;i++)
System.out.print(n[i]+“ ”);

RESULT: The above java program is successfully executed.

PROGRAM 29:
AIM:
Write a program in java to arrange the array elements in descending order
using BubbleSort technique.
ALGORITHM:
1)Start the program.
2)lmport the java package.

import java.util.Scanner;

3)Declare the class,object and main function.

class BubbleSort

4)Declare and initiatize the variables.

int arr[ ]=new int[6];


int i,j,temp;

5)Accept the array elements.

for(i=0;i<6;i++)
arr[i]=in.nextlnt();

6)From i=0 to i<5 repeat step 7 to step 9.


7)From j=0 to j<5-i repeat step 8 to step 9. 8)lf(arr[j]<arr[j+1]) then goto step 9.
9)temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;
10) Print the sorted array.
11)End the program.
CODING:
import java.util.Scanner;
class BubbleSort
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int arr[]=new int[6];
int i,j,temp;
System.out.println("Enter the array elements:"); for(i=0;i<6;i++)
arr[i]=in.nextlnt();
for(i=0;i<5;i++){
for(j=0;j<5-i;j++){
if(arr[j]<arr[j+1])
{temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;}
}}
System.out.println("The numbers in descending order are:");
for(i=0;i<6;i++)
System.out.print(arr[i]+ ” ”);
}}

RESULT: The above java program is successfully executed.

PROGRAM 30:
AIM:
Write a java program to insert an array element at a particular position.
ALGORITHM:
1)Start the program.
2)lmport the java package.

import java.util.*;

3)Declare the class,object and main function.

class Insertion

4)Declare and initialize the variables.

int n,pos,x;

5)Accept the no. of array elements.

n=in.nextlnt();

6)int a[ ]=new int[n+1];

7)Accept the input from the user.

for(int i=0;i<n;i++)
a[i]=in.nextlnt();
pos=in.nextlnt();
x=in.nextlnt();
8)From i=n-1 to i>=pos-1 repeat step 9.
9)a[i+1]=a[i]
10)a[pos-1]=x;
11)Print the array after insertion.
12)End the program.

CODING:
import java.util.*;
public class lnsertion{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int n,pos,x;
System.out.println("Enter the no. of elements you want In array:");
n=in.nextlnt();
int a[]=new int[n+1];
System.out.println("Enter the array elements:");
for(int i=0;i<n;i++)
a[i]=in.nextlnt();
System.out.println("Enter the position where you want to insert element:");
pos=in.nextlnt();
System.out.println("Enter the element you want to insert:");
x=in.nextlnt();
for(int i=(n-1);i>=(pos-1);i--)
a[i+1]=a[i];
a[pos-1]=x;
System.out.println("The array after insertion:");
for(int i=0;i<n+1;i++)
System.out.print(a[i]+ " ");
RESULT: The above java program is successfully executed.
PROGRAM 31:
AIM:
Write a java program to merge the two arrays and display the resultant array.
ALGORITHM:
1)Start the program.
2)lmport the java package.

import java.util.*;

3)Declare the class,object and main function.

class Merging

4)Declare and initialize the variables.

int P[ ]=new int[6];


int Q[ ]=new int[4];
int R[ ]=new int[10];
int i,j,k;

5)Accept the input from the user.

for(int i=0;i<6;i++)
P[i]=in.nextlnt();
for(int j=0;j<4;i++)
Q[i]=in.nextlnt();

6)From i=0 to i<6 repeat step 7.


7)R[i]=P[i];
8)k=6;
9)From j=0 to j<4 repeat step 10.
10)R[k]=QU];k++;
11)Print the merged array.
12)End the program.

CODING:
import java.util.*;
class Merging{
public static void main (String args[]){
Scanner in = new Scanner(System.in);
int P[]=new int[6];
int Q[]=new int[4];
int R[]=newint[10];
int i,j,k;
System.out.println("Enter the elements in array P:"); for(i=0;i<6;i++)
P[i]=in.nextlnt();
System.out.println("Enter the elements in array Q:"); for(j=0;j<4;j++)
Q[j]=in.nextlnt();
for(i=0;i<6;i++)
R[i]=P[i];
k=6;
for(j=0;j<4;i++){
R[k]=Q[i];
k++;}
System.out.println("The Merged array is:"); for(i=0;i<10;i++)
System.out.print(R[i]+" ");}}

RESULT: The above java program is successfully executed.


PROGRAM: 32
AIM:
Write a java program to eliminate the duplicates from the array.

ALGORITHM:
1)Start the program.
2)lmport the java package.

import java.util.Scanner;

3)Declare the class,object and main function.

class Duplicate

4)Declare and initialize the variables.

int arr=new int[50];


int size,i,j;

5)Accept the input from the user.

size=in.nextlnt();
for(i=0;i<size;i++)
arr[i]=in.nextlnt();

6)From i=0 to i<size repeat step 7 to step 11.


7)From j=i+1 to j<size repeat step 8 to step 11.
8)lf(arr[i]==arr[j]) perform the step 9 to step 11.
9)Until j<size-1 repeat step 10.
10)arr[j]=arr[j+1];j++;
11)size--;
12)Print the array after removing duplicates.
13)End the program.

CODING:
import java.util.Scanner;
public class Duplicate{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int arr[]=new int[50];
int size,i,j;
System.out.println("Enter the array size:"); size=in.nextlnt();
System.out.println("Enter the array elements:"); for(i=0;i<size;i++)
arr[i]=in.nextlnt();
for(i=0;i<size;i++){
for(j=i+1;j<size;j++){
if(arr[i]==arr[j]){
while(j<size-1){
arr[j]=arr[j+1];
j++;}
size--;}}}
System.out.println("Array after removing duplicates:"); for(i=0;i<size;i++)
System.out.print(arr[i]+ " ");
}}

RESULT: The above java program is successfully executed.

PROGRAM 33:
AIM:
Write a java program to check if a given number is a magic number.
ALGORITHM:
1)Start the program.
2)lmport the java package.

import java.util.*;

3)Declare the class and instance variables.

public class Magic{


int n;

4)Dec|are a constructor to initialize the data member.

n=0;

5)Declare a function void getnum( ) to accept the number.

n=in.nextInt( );

6)Declare a function int sum_digits(int x) which returns the sum of the digits of
the number x using the recursive technique.

7) if(x==0)
return 0;
else{
int d=x%10;
return(d+sum_digits(x/10));}
8)Declare a function void showMagic( ) which checks if the given number is a
magic number be calling the function sum_digits(int) and display the
appropriate message.

9) int no=n;
while(no>9)
no=sum_digits(no);
if(no==1)
System.out.println(n+ “is a Magic number”); System.out.println(n+ “is
not a Magic number”);

10)Define a main( ) function to create an object. 11)Call the methods to check


for magic number.

br.getnum( );
br.showMagic( );

12)End the program.

CODING:
import java.util.*;
public class Magic
{
int n;
public Magic(){
n=0;}
public void getnum()
{
Scanner in=new Scanner(System.in); System.out.println("Enter the number:");
n=in.nextlnt();
}
public int sum_digits(int x)
{
if(x==0)
return 0;
else{
int d=x%10;
return(d+sum_digits(x/10));}
}
public void showMagic()
{
int no=n;
while(no>9){
no=sum_digits(no);}
if(no==1)
System.out.println(n+ "is a Magic number.");
else
System.out.println(n+ "is not a Magic number.");
}
public static void main(String args[])
{
Magic br=new Magic();
br.getnum();
br.showMagic();
}}

RESULT: The above java program is successfully executed.


PROGRAM 34:
AIM:
Write a program in java to calculate the value of Permutation and
Combination.
ALGORITHM:
1)Start the program.
2)lmport the java package.

import java.util.Scanner;

3)Declare the class and instance variables.

public class CombinationPermutation{


int n,r;
double Cmb,Pmt;

4)Declare a constructor to initialize the data members.

n=0;r=0;Cmb=0.0;Pmt=0.0;

5)Declare a function void read( ) to accept the values of the data members.

n=in.nextlnt( );
r=in.nextlnt( );

6)Declare a function int fact(int) which returns the factorial of a number using
recursion.

7) if(num==0) return 1;
else return(num*fact(num-1));
8)Declare a function void compute( )which calculate the value of permutation
and combination

9) Cmb=(double)fact(n)/((double)fact(n-r)*(double)fact(r));
Pmt=(double)fact(n)/(double)fact(n-r);
10)Declare another function void dlsplay( ) to show the result.
11)Print the result.
12)Declare the main( ) function to create an object.
13)Call the methods accordingly to calculate and display the values of
permutation and combination.

pc.read( );
pc.compute( );
pc.display( );

14)End the program.

CODING:
import java.util.Scanner;
public class CombinationPermutation{
int n,r;
double Cmb,Pmt;
public CombinationPermutation(){
n=0;
r=0;
Cmb=0.0;
Pmt=0.0;}
public void read(){
Scanner in=new Scanner(System.in);
System.out.println("Enter the value of n and r:");
n=in.nextlnt();
r=in.nextlnt();}
public int fact(int num)
{
if(num==0)
return 1;
else
return(num*fact(num-1));
}
public void compute()
{
Cmb=(double)fact(n)/((double)fact(n-r)*(double)fact(r));
Pmt=(double)fact(n)/(doub|e)fact(n-r);
}
public void display()
{
System.out.println("C(" +n+ "," +r+ ")=" +Cmb); System.out.println("P(" +n+ ","
+r+ ")=" +Pmt);
}
public static void main(String args[])
{
CombinationPermutation pc=new CombinationPermutation();
pc.read();
pc.compute();
pc.display();
}}
RESULT: The above java program is successfully executed.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy