Problem: Java Program To Solve Tower
Of Hanoi Problem Using Stacks
import java.util.*;
public class TowerOfHanoiUsingStacks {
public static int N;
@SuppressWarnings("unchecked")
public static Stack<Integer>[]
tower = new Stack[4];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
tower[1] = new Stack<Integer>();
tower[2] = new Stack<Integer>();
tower[3] = new Stack<Integer>();
System.out.print("Enter The Number
Of Disks: ");
int num = scan.nextInt();
N = num;
toh(num);
scan.close();
}
/* Function to push disks into stack */
public static void toh(int n) {
for (int d = n; d > 0; d--)
tower[1].push(d);
display();
move(n, 1, 2, 3);
}
public static void move(int n, int a,
int b, int c) {
if (n > 0) {
move(n-1, a, c, b);
int d = tower[a].pop();
tower[c].push(d);
display();
move(n-1, b, a, c);
}}
public static void display() {
System.out.println(" A
| B |
C");
System.out.println("---------------");
for(int i = N - 1; i >= 0; i--) {
String d1 = " ", d2 = "
", d3 = " ";
try {
d1 = String.valueOf(tower[1].get(i)); }
catch (Exception e){
}
try {
d2 = String.valueOf(tower[2].get(i)); }
catch(Exception e){
}
try {
d3 = String.valueOf(tower[3].get(i)); }
catch (Exception e){
}
System.out.println(" "+d1+" |
"+d2+" | "+d3);
}
System.out.println("\n");
}}
Problem: Java Program To Implement
Linear Search Or Sequential Search Algorithm
import java.util.Scanner;
class LinearSearch {
public static void main(String[] args){
int c, n, search, array[];
Scanner input = new Scanner(System.in);
System.out.print("Enter Total No.
Of Integers: ");
n = input.nextInt();
array = new int[n];
System.out.println("Enter " +
n + " Integers: ");
for (c = 0; c < n; c++)
array[c] = input.nextInt();
System.out.print("Enter Value To
Find: ");
search = input.nextInt();
for (c = 0; c < n; c++){
if (array[c] == search){ /* searching
element is present */
System.out.print(search + " Is
Present At Location " + (c + 1) + ".");
break;
}}
if (c == n) /* searching element is absent */
System.out.println(search + " Is
Not Present An Array.");
input.close();
}}