blueJ project
blueJ project
blueJ project
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;
5) Long num,savNum;
int digit,sumDigit=0,sumPrime=0,factor,
countOfFactors=0,prodFactors=1,i;
num=in.nextLong();
Digits=(int)savNum%10;
sumDigit+=digit;
savNum/=10;
Digit=(int)savNum%10;
sumPrime+=digit;
savNum/=10;
Import java.util.Scanner;
int n
long n1,n2,a;
long CatNumber;
n=in.nextInt();
if(n>=1&&n<=10)
longfactOfa=1,factOf2a=1;
for(long i=1;i<=a;i++)
factOfa*=i;
for(long i=1;i<=2*a;i++)
factOf2a*=i;
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);}}}}
import java.util.Scanner;
int n;
int s=0;
n=in.nextInt();
if(n>0)
if(n%i==0)
s+=i;
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
importjava.util.Scanner;
intR[]=new int[16];
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;
class Spiral
int[] []a;
int N;
4) Receive the array variable.
sp.printNormal();
sp.printSpiral();
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;
Boolean a[ ][ ];
RelPrime()
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;
}}}
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);
}}
import java.util.Scanner;
void accept( )
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( );
}
}
import java.io.*;
Class personal
String Name;
int pan;
double basic-pay;
int acc-no;
int yrs;
double pf;
double grat;
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);
}
}
}
import java.io.*;
Class node
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---“);
}
}
importjava.util.Scanner;
intmid,n;
int mid=n/2;
for(int i=2;i<=mid;i++)
if(n%i==0)
return false;
return true;
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
importjava.util.Scanner;
intmid,n;
int mid=n/2;
for(int i=2;i<=mid;i++)
if(n%i==0)
return false;
return true;
for(inti=3;i<=lim;i+=2)
if(IsPrime(i)==true&&IsPrime(i+2)==true)
System.out.println(“(“+i+”,”+(i+2)+”)”);
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;
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);
}}
importjava.util.*;
int n;
long f=1;
for(inti=2;i<=n;i++)
f=f*i;
return f;
6) Value of sin x
7) Value of cos x
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”);
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.*
Class plane
int x1,y1;
X1=nx;
Y1=ny;
int x2,y2;
doubleradius,area;
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
Class Record
String [] names;
int [] rnk;
names=new String[5];
rnk= new int[5];
int index;
index=0;
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]);
}
}
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.*;
class stock
String item;
intqty,rate,amt;
intpqty,prate;
amt=qty*rate;
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;
qty +=pqty;
if(prate !=rate)
rate=prate;
amt==qty*rate;
}
void display();
update();
super.display();
}
}
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.*;
class worker
String name;
double basic;
Return hours*rate;
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);
}
}
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;
intR[]=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;
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;
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.*;
Class CircularPrime
int n,num,digit=0,p,n1,c,k=0;
n=in.nextlnt();
7) num=n;
p=(int)Math.pow(10,digit-1);
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”);
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");
}
}
import java.util.*;
int n,num,r,c=0;
String s:” ”;
long bin;
n=in.nextlnt();
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”);
}
}
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.*;
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 +",");}
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();
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();
}
}
import java.io.*;
s=in.readLine();
6) s=s+ ” ”;
l=s.length();
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+" ".
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="";
}
}
}
}
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.*;
String str;
char c,ch;
int i,j,l,freq=0,f=-1;
str=in.readLine( );
6) str=str.toUpperCase();
7)Find the length of the string.
l=str.length();
12)ch=str.charAt(j);
13)if(c==ch)
freq=freq+1;
16)lf(f==-1)
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;
}
}
}
ALGORITHM:
1)Start the program.
2)lmport the java package
import java.io.*;
s=in.readLine( );
6)s=s+ ” ”;
9)c=str.charAt(i);
11)st=st+c;
12)wrd[k++]=st;
st= "";
13)From i=0 to i<k-1 repeat step 14 to step 16.
18)str=wrd[i]+ ” ”;
19)Print str.
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);
}
}
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.*;
s=in.readLine( );
6) s=s.toUpperCase();s=s+ “ ”;
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);
}}
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.*;
5)Accept the array elements and no. to be searched from the user.
for(i=0;i<6;i++)
n[i]=in.nextlnt();
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");
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;
class BinarySearch
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();
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."); ,
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.");
}
}
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;
class SelectionSort
for(i=0;i<6;i++)
n[i]=in.nextlnt();
7)min=i;
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]+“ ”);
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;
class BubbleSort
for(i=0;i<6;i++)
arr[i]=in.nextlnt();
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.*;
class Insertion
int n,pos,x;
n=in.nextlnt();
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.*;
class Merging
for(int i=0;i<6;i++)
P[i]=in.nextlnt();
for(int j=0;j<4;i++)
Q[i]=in.nextlnt();
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]+" ");}}
ALGORITHM:
1)Start the program.
2)lmport the java package.
import java.util.Scanner;
class Duplicate
size=in.nextlnt();
for(i=0;i<size;i++)
arr[i]=in.nextlnt();
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]+ " ");
}}
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.*;
n=0;
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”);
br.getnum( );
br.showMagic( );
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();
}}
import java.util.Scanner;
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( );
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.