1■■ Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "Priyanshu";
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--) {
rev += [Link](i);
}
[Link]("Reversed String: " + rev);
}
}
2■■ Factorial of a Number (Iteration & Recursion)
// Iterative
public class FactorialIterative {
public static void main(String[] args) {
int n = 5, fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
[Link]("Factorial (Iterative): " + fact);
}
}
// Recursive
public class FactorialRecursive {
static int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
int n = 5;
[Link]("Factorial (Recursive): " + factorial(n));
}
}
3■■ Check Palindrome (String or Number)
// String Palindrome
public class PalindromeString {
public static void main(String[] args) {
String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
if ([Link](rev))
[Link]("Palindrome String");
else
[Link]("Not a Palindrome");
}
}
// Number Palindrome
public class PalindromeNumber {
public static void main(String[] args) {
int num = 121, temp = num, rev = 0;
while (num > 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
if (temp == rev)
[Link]("Palindrome Number");
else
[Link]("Not a Palindrome");
}
}
4■■ Fibonacci Series
public class FibonacciSeries {
public static void main(String[] args) {
int n = 10;
int a = 0, b = 1;
[Link]("Fibonacci Series: " + a + " " + b);
for (int i = 2; i < n; i++) {
int c = a + b;
[Link](" " + c);
a = b;
b = c;
}
}
}
5■■ Sort Strings Without Using Inbuilt Functions
public class SortStrings {
public static void main(String[] args) {
String[] arr = {"banana", "apple", "mango", "cherry"};
for (int i = 0; i < [Link]; i++) {
for (int j = i + 1; j < [Link]; j++) {
if (arr[i].compareTo(arr[j]) > 0) {
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
[Link]("Sorted Strings:");
for (String s : arr)
[Link](s);
}
}
6■■ Find Duplicates in an Array
public class DuplicateElements {
public static void main(String[] args) {
int[] arr = {2, 4, 6, 2, 8, 4, 10};
[Link]("Duplicate elements:");
for (int i = 0; i < [Link]; i++) {
for (int j = i + 1; j < [Link]; j++) {
if (arr[i] == arr[j]) {
[Link](arr[i]);
break;
}
}
}
}
}
7■■ Count Vowels and Consonants in a String
public class VowelConsonantCount {
public static void main(String[] args) {
String str = "Priyanshu";
str = [Link]();
int vowels = 0, consonants = 0;
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if (ch >= 'a' && ch <= 'z') {
if ("aeiou".indexOf(ch) != -1)
vowels++;
else
consonants++;
}
}
[Link]("Vowels: " + vowels);
[Link]("Consonants: " + consonants);
}
}
8■■ Check Prime Number
public class PrimeCheck {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
if (num <= 1)
isPrime = false;
else {
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
[Link](num + " is a Prime Number");
else
[Link](num + " is Not a Prime Number");
}
}
9■■ Swap Two Numbers Without Using Third Variable
public class SwapNumbers {
public static void main(String[] args) {
int a = 10, b = 20;
[Link]("Before Swap: a=" + a + " b=" + b);
a = a + b;
b = a - b;
a = a - b;
[Link]("After Swap: a=" + a + " b=" + b);
}
}
■ Find Largest and Smallest Element in an Array
public class LargestSmallest {
public static void main(String[] args) {
int[] arr = {10, 25, 5, 40, 15};
int largest = arr[0];
int smallest = arr[0];
for (int num : arr) {
if (num > largest)
largest = num;
if (num < smallest)
smallest = num;
}
[Link]("Largest: " + largest);
[Link]("Smallest: " + smallest);
}
}