Panda
Panda
import java.util.Arrays;
reverseArray(numbers);
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
FINDING SMALLEST AND LARGEST IN AN ARRAY
smallest = number;
largest = number;
SUM OF ELEMENTS
return sum;
Factorial
int number = 5; // You can change this number to test with other values
return result;
RECUSRIVE IN FACTORIAL
int number = 5; // You can change this number to test with other values
if (n == 0) {
} else {
import java.util.Arrays;
Arrays.sort(array);
// Compare the product of two largest numbers and two smallest numbers
int n = array.length;
int product1 = array[n - 1] * array[n - 2]; // Product of the two largest numbers
int product2 = array[0] * array[1]; // Product of the two smallest numbers (negative)
ANAGRAM
import java.util.Arrays;
if (str1.length() != str2.length()) {
return false;
Arrays.sort(arr1);
Arrays.sort(arr2);
PALINDROME
String str = "madam"; // You can test with other strings as well
}
public static boolean isPalindrome(String str) {
int left = 0;
if (str.charAt(left) != str.charAt(right)) {
left++;
right--;
PATTERNS
In Java, patterns can be printed using loops. Common patterns include stars (*), numbers, and
alphabets arranged in different shapes, such as triangles, squares, and pyramids.
Below are some popular star patterns with Java code examples:
**
***
****
*****
Java Code:
System.out.print("*");
*****
****
***
**
Java Code:
System.out.print("*");
3. Pyramid Pattern
*
***
*****
*******
*********
Java Code:
// Print spaces
System.out.print(" ");
// Print stars
System.out.print("*");
4. Inverted Pyramid
*********
*******
*****
***
Java Code:
// Print spaces
System.out.print(" ");
// Print stars
System.out.print("*");
5. Diamond Pattern
***
*****
*******
*********
*******
*****
***
Java Code:
// Print spaces
for (int j = i; j < n; j++) {
System.out.print(" ");
// Print stars
System.out.print("*");
// Print spaces
System.out.print(" ");
// Print stars
System.out.print("*");
*****
* *
* *
* *
*****
Java Code:
public class HollowSquarePattern {
if (i == 1 || i == n || j == 1 || j == n) {
System.out.print("*");
} else {
System.out.print(" ");
System.out.println();
23
456
7 8 9 10
11 12 13 14 15
Java Code:
num++;
}
System.out.println(); // Move to the next line
1. Reverse a String
if (str.equals(reversed)) {
System.out.println("Palindrome");
} else {
System.out.println("Not a Palindrome");
char ch = 'l';
int count = 0;
if (c == ch) {
count++;
5. Factorial of a Number
int num = 5;
int fact = 1;
6. Fibonacci Series
int n = 5;
int a = 0, b = 1;
int c = a + b;
a = b;
b = c;
secondMax = max;
max = num;
secondMax = num;
int num = 7;
if (num % i == 0) {
isPrime = false;
break;
int total = n * (n + 1) / 2;
int sum = 0;
sum += num;
int sum = 0;
num /= 10;
while (temp != 0) {
temp /= 10;
if ("aeiou".indexOf(ch) != -1) {
vowels++;
} else {
consonants++;
int temp = b;
b = a % b;
a = temp;
import java.util.Arrays;
Arrays.sort(arr);
int sum = 0;
sum += num;
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
System.out.println("Matrix Multiplication Result: ");
System.out.println(Arrays.toString(row));
import java.util.HashSet;
int maxLength = 0;
int left = 0;
while (set.contains(str.charAt(right))) {
set.remove(str.charAt(left));
left++;
set.add(str.charAt(right));
import java.util.Arrays;
public class Anagram {
Arrays.sort(arr1);
Arrays.sort(arr2);
if (Arrays.equals(arr1, arr2)) {
System.out.println("Anagrams");
} else {
System.out.println("Not Anagrams");
int sum = 0;
sum += num;
number patterns
System.out.println();
Output:
12
123
1234
12345
}
System.out.println();
Output:
12345
1234
123
12
System.out.print(" ");
System.out.print(j);
System.out.println();
Output:
121
12321
1234321
123454321
System.out.print(" ");
System.out.print(j);
System.out.println();
Output:
123454321
1234321
12321
121
System.out.print(j);
System.out.println();
System.out.print(" ");
System.out.print(j);
System.out.println();
Output:
121
12321
1234321
123454321
1234321
12321
121
int num = 1;
num++;
System.out.println();
Output:
23
456
7 8 9 10
11 12 13 14 15
if (j == 0 || j == i) {
arr[i][j] = 1;
} else {
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
System.out.println();
Output:
11
121
1331
14641
System.out.println();
Output:
22
333
4444
55555
Mirroing of letters
. // Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
int l = str.length();
return answer;
// Driver function
int n = 2;
System.out.print(compute(str, n - 1));
}
Rearrange positive and negative
// numbers in an array
import java.util.Arrays;
class GfG {
if (arr[i] > 0)
continue;
arr[j + 1] = arr[j]; // Shift the positive element one position to the right
arr[j + 1] = temp;
rearrange(arr);
String Manipulation
import java.util.HashMap;
import java.util.Map;
class Solution {
valueMap.put('A', 1);
valueMap.put('B', 10);
valueMap.put('C', 100);
valueMap.put('D', 1000);
valueMap.put('E', 10000);
valueMap.put('F', 100000);
valueMap.put('G', 1000000);
int sum = 0;
// Loop through each character in the string and sum their corresponding values
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// If the character is a valid key in the map, add its value to the sum
if (valueMap.containsKey(ch)) {
sum += valueMap.get(ch);
return sum;
// Example usage