









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Java implementations of basic programming problems. Includes solutions for string reversal, palindrome checks, Fibonacci series generation, prime number checks, vowel/consonant counting, array sorting/merging, Armstrong number checks, number reversal, GCD calculation, anagram checks, prime number range printing, finding the largest of three numbers, number swapping, Pascal's triangle printing, missing number finding, decimal-to-binary conversion, perfect number checks, simple calculator creation, digit sum calculation, string length finding, character occurrence counting, first non-repeated character finding, whitespace removal, common element finding in arrays, factorial calculation using recursion, leap year checks, sum of first n natural numbers, simple login creation, substring checks, max occurring character finding, bubble sort, and selection sort.
Typology: Cheat Sheet
1 / 17
This page cannot be seen from the preview
Don't miss anything!
public class ReverseString { public static void main(String[] args) { String str = "Automation"; StringBuilder reversed = new StringBuilder(str).reverse(); System.out.println(reversed); } }
public class Palindrome { public static void main(String[] args) { String str = "madam"; String reversed = new StringBuilder(str).reverse().toString(); System.out.println(str.equals(reversed)); } }
public class Fibonacci { public static void main(String[] args) { int n = 10, num1 = 0, num2 = 1; System.out.print("Fibonacci Series: " + num1 + ", " + num2); for (int i = 2; i < n; i++) { int num3 = num1 + num2; System.out.print(", " + num3); num1 = num2; num2 = num3; } }
public class Factorial { public static void main(String[] args) { int num = 5, factorial = 1; for (int i = 1; i <= num; i++) { factorial *= i; } System.out.println(factorial); } }
public class PrimeCheck { public static void main(String[] args) { int num = 11; boolean isPrime = true; for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { isPrime = false; break; } } System.out.println(isPrime); } }
public class VowelConsonantCount { public static void main(String[] args) { String str = "Automation";
System.out.println(Arrays.toString(merged)); } }
public class LargestInArray { public static void main(String[] args) { int[] arr = {1, 3, 5, 7, 9}; int largest = arr[0]; for (int num : arr) { if (num > largest) { largest = num; } } System.out.println(largest); } }
import java.util.HashSet; public class RemoveDuplicates { public static void main(String[] args) { int[] arr = {1, 2, 2, 3, 4, 4}; HashSet
public class ArmstrongNumber { public static void main(String[] args) { int num = 153, sum = 0, temp = num; while (temp != 0) { int digit = temp % 10; sum += Math.pow(digit, 3); temp /= 10; } System.out.println(num == sum); } }
public class ReverseNumber { public static void main(String[] args) { int num = 12345, reversed = 0; while (num != 0) { reversed = reversed * 10 + num % 10; num /= 10; } System.out.println(reversed); } }
public class GCD { public static void main(String[] args) { int a = 60, b = 48; while (b != 0) { int temp = b;
int start = 10, end = 50; for (int num = start; num <= end; num++) { boolean isPrime = true; for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { isPrime = false; break; } } if (isPrime && num > 1) { System.out.print(num + " "); } } } }
public class SecondLargest { public static void main(String[] args) { int[] arr = {12, 35, 1, 10, 34, 1}; int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE; for (int num : arr) { if (num > first) { second = first; first = num; } else if (num > second && num != first) { second = num; } } System.out.println(second); }
public class SwapNumbers { public static void main(String[] args) { int a = 5, b = 10; a = a + b; b = a - b; a = a - b; System.out.println("a: " + a + ", b: " + b); } }
public class PascalsTriangle { public static void main(String[] args) { int rows = 5; for (int i = 0; i < rows; i++) { int num = 1; System.out.format("%" + (rows - i) * 2 + "s", ""); for (int j = 0; j <= i; j++) { System.out.format("%4d", num); num = num * (i - j) / (j + 1); } System.out.println(); } } }
public class MissingNumber { public static void main(String[] args) {
import java.util.Scanner; public class SimpleCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter first number: "); double num1 = scanner.nextDouble(); System.out.print("Enter second number: "); double num2 = scanner.nextDouble(); System.out.print("Enter operation (+, - , , /): "); char operation = scanner.next().charAt(0); double result; switch (operation) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '': result = num1 * num2; break; case '/': result = num1 / num2; break; default: throw new IllegalArgumentException("Invalid operation"); } System.out.println("Result: " + result); } }
public class SumOfDigits { public static void main(String[] args) { int num = 12345, sum = 0; while (num != 0) { sum += num % 10; num /= 10; }
System.out.println(sum); } }
public class StringLength { public static void main(String[] args) { String str = "Automation"; System.out.println(str.length()); } }
public class CheckEmptyString { public static void main(String[] args) { String str = ""; System.out.println(str.isEmpty()); } }
public class CountCharacter { public static void main(String[] args) { String str = "Automation"; char ch = 'a'; int count = 0; for (char c : str.toCharArray()) { if (c == ch) count++; } System.out.println(count); } }
public static void main(String[] args) { int[] arr1 = {1, 2, 3, 4}; int[] arr2 = {3, 4, 5, 6}; HashSet
public class FactorialRecursion { public static void main(String[] args) { int num = 5; System.out.println(factorial(num)); } static int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); } }
import java.util.Random; public class RandomNumbers {
public static void main(String[] args) { Random random = new Random(); for (int i = 0; i < 5; i++) { System.out.println(random.nextInt(100)); // Random number between 0- 99 } } }
public class LeapYear { public static void main(String[] args) { int year = 2024; boolean isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); System.out.println(isLeap); } }
public class SumOfNaturalNumbers { public static void main(String[] args) { int n = 10, sum = n * (n + 1) / 2; System.out.println(sum); } }
import java.util.Scanner; public class SimpleLogin { public static void main(String[] args) { String username = "admin"; String password = "password"; Scanner scanner = new Scanner(System.in);
int maxCount = 0; for (char c : charCount.keySet()) { if (charCount.get(c) > maxCount) { maxCount = charCount.get(c); maxChar = c; } } System.out.println(maxChar); } }
public class BubbleSort { public static void main(String[] args) { int[] arr = {64, 34, 25, 12, 22, 11, 90}; int n = arr.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } for (int num : arr) { System.out.print(num + " "); } } }
public class SelectionSort { public static void main(String[] args) { int[] arr = {64, 25, 12, 22, 11}; int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; } for (int num : arr) { System.out.print(num + " "); } } }