Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Basic Programming Problems in Java: Code Examples and Solutions, Cheat Sheet of Computer Science

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

2020/2021

Available from 05/31/2025

rachel-hephz
rachel-hephz 🇮🇳

3 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "Automation";
StringBuilder reversed = new StringBuilder(str).reverse();
System.out.println(reversed);
}
}
2. Check for Palindrome
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));
}
}
3. Fibonacci Series
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;
}
}
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Basic Programming Problems in Java: Code Examples and Solutions and more Cheat Sheet Computer Science in PDF only on Docsity!

1. Reverse a String

public class ReverseString { public static void main(String[] args) { String str = "Automation"; StringBuilder reversed = new StringBuilder(str).reverse(); System.out.println(reversed); } }

2. Check for Palindrome

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)); } }

3. Fibonacci Series

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; } }

4. Factorial of a Number

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); } }

5. Prime Number Check

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); } }

6. Count Vowels and Consonants

public class VowelConsonantCount { public static void main(String[] args) { String str = "Automation";

System.out.println(Arrays.toString(merged)); } }

9. Find the Largest Element in an Array

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); } }

10. Remove Duplicates from an Array

import java.util.HashSet; public class RemoveDuplicates { public static void main(String[] args) { int[] arr = {1, 2, 2, 3, 4, 4}; HashSet set = new HashSet<>(); for (int num : arr) { set.add(num); } System.out.println(set); } }

11. Check if a Number is Armstrong

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); } }

12. Reverse a Number

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); } }

13. Calculate GCD of Two Numbers

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 + " "); } } } }

17. Find the Second Largest Element in an Array

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); }

18. Swap Two Numbers

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); } }

19. Print the Pascal's Triangle

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(); } } }

20. Find the Missing Number in an Array

public class MissingNumber { public static void main(String[] args) {

23. Implementing a Simple Calculator

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); } }

24. Find the Sum of Digits of a Number

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); } }

25. Find the Length of a String

public class StringLength { public static void main(String[] args) { String str = "Automation"; System.out.println(str.length()); } }

26. Check if a String is Empty

public class CheckEmptyString { public static void main(String[] args) { String str = ""; System.out.println(str.isEmpty()); } }

27. Count the Occurrences of a Character in a String

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 set = new HashSet<>(); for (int num : arr1) { set.add(num); } for (int num : arr2) { if (set.contains(num)) { System.out.print(num + " "); } } } }

31. Find the Factorial of a Number using Recursion

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); } }

32. Generate Random Numbers

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 } } }

33. Check if a Year is Leap Year

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); } }

34. Find the Sum of First N Natural Numbers

public class SumOfNaturalNumbers { public static void main(String[] args) { int n = 10, sum = n * (n + 1) / 2; System.out.println(sum); } }

35. Implement a Simple Login System

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); } }

38. Implementing Bubble Sort

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 + " "); } } }

39. Implementing Selection Sort

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 + " "); } } }