1) Hello World
public class
HelloWorld {
public static void
main(String[] args)
{ [Link]("He
llo, World!");
}
}
Output
Hello,
World!
2) Calculator
import
[Link]
ner; public
class
Calculator {
public static void
main(String[] args) {
Scanner sc = new
Scanner([Link]);
[Link]("Enter
first number: "); double
a = [Link]();
[Link]("Enter
second number: ");
double b =
[Link]();
[Link]("Enter
operator (+ - * /): "); char
op = [Link]().charAt(0);
double result = 0;
switch(op){
case '+': result
= a+b; break;
case '-': result
= a-b; break;
case '*': result
= a*b; break;
case '/': result
= a/b; break;
default: [Link]("Invalid
operator");
}
[Link]("Result = " + result);
}
}
Output:
Result
= 50.0
3) Factorial (Iterative & Recursive)
public class Factorial {
static int
recursiveFact(int
n){ if(n==0)
return 1;
return n * recursiveFact(n-1);
}
static int
iterativeFact(int
n){ int fact = 1;
for (int i=1;
i<=n; i++)
fact *= i;
return fact;
}
public static void
main(String[] args)
{ int n = 5;
[Link]("Recursive factorial: " +
recursiveFact(n));
[Link]("Iterative factorial: " +
iterativeFact(n));
}
}
Output:
Recursive factorial: 120
Iterative factorial: 120
4) Palindrome String
public class Palindrome {
public static void
main(String[] args)
{ String s =
"madam";
String rev = "";
for(int
i=[Link]()-1;
i>=0; i--) rev +=
[Link](i);
if([Link](rev))
[Link](s + " is a palindrome.");
else
[Link](s + " is not a
palindrome.");
}
}
Output:
madam is a palindrome.
5) Fibonacci Series
public class Fibonacci {
public static void
main(String[] args)
{ int n = 10;
int a = 0, b = 1;
[Link]("Fi
bonacci: "); for(int
i=1; i<=n; i++){
[Link]
t(a + " "); int
sum = a + b;
a = b;
b = sum;
}
}
}
Output:
Fibonacci: 0 1 1 2 3 5 8 13 21 34
6) Sorting Algorithms (Bubble, Selection, Insertion)
// Bubble Sort Example
public class SortAlgorithms {
public static void
main(String[] args)
{ int[] arr = {5, 2,
8, 1, 3};
for(int
i=0;i<[Link]-
1;i++) for(int
j=0;j<[Link]-
i-1;j++)
if(arr[j] >
arr[j+1]){
int temp =
arr[j];
arr[j] =
arr[j+1];
arr[j+1] =
temp;
}
[Link]("Bubble
Sorted: "); for(int
n:arr)
[Link](n+" ");
}
}
Output:
Bubble Sorted: 1 2 3 5 8
7) Linear Search
public class LinearSearch {
public static void
main(String[] args)
{ int[] arr = {10,
20, 30, 40};
int key = 30;
for(int i=0;
i<[Link]; i++)
{ if(arr[i] ==
key){
[Link]("Found at
index: " + i); return;
}
}
[Link]("Not found");
}
}
Output:
Found at index: 2
8) Binary Search
public class BinarySearch {
public static void
main(String[] args)
{ int[] arr = {10,
20, 30, 40, 50};
int key = 40;
int l=0, r=[Link]-1;
while(l<=r){
int mid =
(l+r)/2;
if(arr[mid] ==
key){
[Link]("Found at
index: " + mid); return;
}
else if(arr[mid] <
key) l = mid + 1; else
r = mid - 1;
}
[Link]("Not found");
}
}
Output:
Found at index: 3
9) String Operations
public class StringOps {
public static void
main(String[] args)
{ String a =
"Hello";
String b = "World";
[Link]("Substring: "
+ [Link](1,4));
[Link]("Concatenatio
n: " + a + b);
[Link]("Length: " +
[Link]());
}
}
Output:
Substrin
g: ell
Concatenation:
HelloWorld
Length: 5
10) Singly Linked List
class Node {
int
data;
Node
next;
Node(int d){ data = d; }
}
public class
LinkedList {
Node head;
void insert(int data){
Node n = new
Node(data);
if(head == null)
head = n; else{
Node temp = head;
while([Link] != null)
temp = [Link];
[Link] = n;
}
}
void
display(
){ Node
t =
head;
while(t != null)
{ [Link]
t([Link] + " ");
t = [Link];
}
}
public static void
main(String[] args)
{ LinkedList l = new
LinkedList();
[Link](10);
[Link](20);
[Link](30);
[Link]();
}
}
Output:
10 20 30
11) Stack Using Array
public class
StackArray {
int top = -
1;
int[] stack = new int[5];
void
push(int
x)
{ stack[+
+top] =
x;
}
int pop(){
return stack[top--];
}
void display(){
for(int i=0; i<=top; i++)
[Link](stack[i]+" ");
}
public static void
main(String[] args)
{ StackArray s = new
StackArray();
[Link](10);
[Link](20);
[Link](30);
[Link]();
}
}
Output:
10 20 30
12) Queue Using Linked List
class QNode
{
int data;
QNode next;
QNode(int d){ data=d; }
}
public class
QueueLL
{ QNode
front,
rear;
void enqueue(int d){
QNode n = new QNode(d);
if(rear == null)
front = rear = n;
else{
[Link]
= n; rear
= n;
}
}
void
display()
{ QNode t
= front;
while(t != null)
{ [Link]
nt([Link]+" ");
t = [Link];
}
}
public static void
main(String[] args)
{ QueueLL q = new
QueueLL();
[Link](10);
[Link]
e(20);
[Link]
e(30);
[Link]
y();
}
}
Output:
10 20 30
13) Binary Tree Traversals
class
TreeNode {
int data;
TreeNode left, right;
TreeNode(int d){
data=d; }
}
public class BinaryTree {
static void
inorder(TreeNode n)
{ if(n == null)
return;
inorder([Link]);
[Link]([Link]
a+" ");
inorder([Link]);
}
static void
preorder(TreeNode n)
{ if(n == null)
return;
[Link]([Link]
a+" ");
preorder([Link]);
preorder([Link]);
}
static void
postorder(TreeNode n){
if(n == null) return;
postorder([Link]);
postorder([Link]);
[Link]([Link]
a+" ");
}
public static void
main(String[] args)
{ TreeNode root = new
TreeNode(10); [Link] =
new TreeNode(5);
[Link] = new
TreeNode(20);
inorder(root);
[Link]();
preorder(root);
[Link]();
postorder(root);
}
}
Output:
5 10 20
10 5 20
5 20 10
14) HashMap Program
import [Link];
public class HashMapDemo {
public static void
main(String[] args)
{ HashMap<String, Integer>
map = new HashMap<>();
[Link]("A", 1);
[Link]("B", 2);
[Link]("Value of A = " +
[Link]("A"));
}
}
Output:
Value of A = 1
15) File Read Program
import [Link].*;
public class FileReadDemo {
public static void
main(String[] args)
{ try{
BufferedReader br = new
BufferedReader(new
FileReader("[Link]")); String line;
while((line =
[Link]()) !=
null)
[Link](
line);
[Link]();
}catch(Exception e){
[Link](e.g
etMessage());
}
}
}
Outpu
t:
Hello
File