Q.no.1: Write a program to perform the function of tower of hanoi…
A.no.1:
import java.io.*;
class tower
{
public void transfer(int n,char from,char to,char temp)
{
if(n>0)
{
transfer(n-1,from,temp,to);
System.out.println("Move disc "+n+" from "+from+" "+"to "+to);
transfer(n-1,temp,to,from);
}
return;
}
public static void main(String args[])throws IOException
{
tower ob=new tower();
int n;
InputStreamReader inp=new InputStreamReader(System.in);
BufferedReader buff=new BufferedReader(inp);
System.out.println("How many discs...???");
n=Integer.parseInt(buff.readLine());
ob.transfer(n,'A','B','C');
}
}
Output:
How many discs...???
3
Move disc 1 from A to B
Move disc 2 from A to C
Move disc 1 from B to C
Move disc 3 from A to B
Move disc 1 from C to A
Move disc 2 from C to B
Move disc 1 from A to B
Algorithm:
Operation— Tower of Hanoi.
Input— Number of discs.
Output— Position of discs.
Steps—
1): define the class name “tower”.
2): start a function “transfer”, with
Q.no.2. Write a program to accept a word or a sentence and display it in PIGLATIN form.
A.no.2.
import java.io.*;
class piglatin
{
InputStreamReader inp=new InputStreamReader(System.in);
BufferedReader buff=new BufferedReader(inp);
private String str;
public void input()throws IOException
{
System.out.println("Enter the String");
str=buff.readLine();
}
public void latin()throws IOException
{
int p,k=0,x=0,y;
String c,d,a="";
char b=' ',ch=' ';
str=str+' ';
p=str.length();
System.out.println("The string in pig latin form: ");
for(int m=0;mb)
{
int c=a;
a=b;
b=c;
}
}
public int rec_hcf(int a,int b)
{
if(b==0)
{
return a;
}
else
{
return rec_hcf(b,a%b);
}
}
public int fn_lcm(int a,int b,int c)
{
int k=a*b;
int d=k/c;
return d;
}
public void result()
{
hcf=rec_hcf(a,b);
System.out.println("HCF...
No comments