Java Programs
Java Programs
Java Programs
// Else no solutions
else
System.out.println("No solutions");
}
// Driver Code
public static void main(String[] args)
{
int a = 2, b = 5, c = 2;
checkSolution(a, b, c);
}
}
StackNode root;
static class StackNode {
int data;
StackNode next;
if (root == null) {
return true;
else
return false;
root = newNode;
else {
root = newNode;
newNode.next = temp;
if (root == null) {
System.out.println("Stack is Empty");
else {
popped = root.data;
root = root.next;
return popped;
}
if (root == null) {
System.out.println("Stack is empty");
return Integer.MIN_VALUE;
else {
return root.data;
// Driver code
sll.push(10);
sll.push(20);
sll.push(30);
System.out.println(sll.pop()
Output
10 pushed to stack
20 pushed to stack
30 pushed to stack
30 popped from stack
Top element is 20
Elements present in stack : 20 10
class Queue {
Queue(int c)
{
front = rear = 0;
capacity = c;
if (capacity == rear) {
System.out.printf("\nQueue is full\n");
return;
else {
queue[rear] = data;
rear++;
return;
}
// if queue is empty
if (front == rear) {
System.out.printf("\nQueue is empty\n");
return;
else {
queue[rear] = 0;
// decrement rear
rear--;
return;
int i;
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
return;
}
// print front of queue
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
queue[front]);
return;
// Driver code
q.queueDisplay();
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
q.queueDisplay();
q.queueEnqueue(60);
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.printf(
"\n\nafter two node deletion\n\n");
q.queueDisplay();
q.queueFront();
Output
Queue is Empty
20 <-- 30 <-- 40 <-- 50 <--
Queue is full
20 <-- 30 <-- 40 <-- 50 <--
40 <-- 50 <--
Front Element is: 40
// Driver Class
class GFG {
int colSize)
System.out.println();
// Function to multiply
{
int i, j, k;
System.out.println("\nMatrix A:");
System.out.println("\nMatrix B:");
if (row2 != col1) {
System.out.println(
return;
System.out.println("\nResultant Matrix:");
// Driver code
int A[][] = { { 1, 1, 1 },
{ 2, 2, 2 },
{ 3, 3, 3 },
{ 4, 4, 4 } };
int B[][] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 } };
Output
Matrix A:
1 1 1
2 2 2
3 3 3
4 4 4
Matrix B:
1 1 1 1
2 2 2 2
3 3 3 3
Resultant Matrix:
6 6 6 6
12 12 12 12
18 18 18 18
24 24 24 24
7b) // Optimized java implementation of Bubble sort
import java.io.*;
class GFG {
int i, j, temp;
boolean swapped;
swapped = false;
temp = arr[j];
arr[j + 1] = temp;
swapped = true;
}
if (swapped == false)
break;
int i;
System.out.println();
// Driver program
{
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = arr.length;
bubbleSort(arr, n);
printArray(arr, n);
Output
Sorted array:
11 12 22 25 34 64 90
import java.util.*;
class GFG{
// Function to calculate
// factorial of N
{
// Base Case
if (n == 1 || n == 0)
return 1;
// Otherwise, recursively
else
int k = (a - 1) / fact(n);
int answer = k;
// Iterate in the range [1, N] and
answer = answer + 1;
return answer;
// Driver Code
int N = 1;
int A = 2;
int R = 3;
System.out.print(findValue(N, R, A));
}
}
Output:
4
try {
System.out.println(
+ " is running");
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
// Main Class
MultithreadingDemo object
= new MultithreadingDemo();
object.start();
Output
Thread 15 is running
Thread 14 is running
Thread 16 is running
Thread 12 is running
Thread 11 is running
Thread 13 is running
Thread 18 is running
Thread 17 is running
11)
// Java Program to Illustrate binarySearch() method
// of Collections class
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
// Main class
al.add(12);
al.add(53);
al.add(23);
al.add(46);
al.add(54);
// index
System.out.print(index);
Output
2
// interface
import java.io.*;
// A simple interface
interface In1 {
void display();
// interface.
System.out.println("Geek");
// Driver Code
t.display();
System.out.println(a);
Output
Geek
10