Fibonacci
public class Fibonacci {
public static void main(String[] args) {
int n1 = 0, n2 = 1, count = 10;
System.out.print(n1 + " " + n2);
for (int i = 2; i < count; i++) {
int n3 = n1 + n2;
System.out.print(" " + n3);
n1 = n2;
n2 = n3;
}
}
}
Fibonacci recursive
Prime or not
public class PrimeNumber {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
Palindrome
public class Palindrome {
public static void main(String[] args) {
int num = 121, reversedNum = 0, remainder, originalNum;
originalNum = num;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
if (originalNum == reversedNum)
System.out.println(originalNum + " is a palindrome.");
else
System.out.println(originalNum + " is not a palindrome.");
}
}
Factorial
public class Factorial {
public static void main(String[] args) {
int num = 5;
long factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
System.out.println("Factorial of " + num + " = " + factorial);
}
}
Armstrong
public class ArmstrongNumber {
public static void main(String[] args) {
int number = 153, originalNumber, remainder, result = 0;
originalNumber = number;
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
}
if (result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}
Generate random number
import java.util.Random;
public class RandomNumber {
public static void main(String[] args) {
Random random = new Random();
int randomNumber = random.nextInt(100); // Generates a random number between 0
and 99
System.out.println("Random Number: " + randomNumber);
}
}
Print pattern
public class PrintPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Compare two objects
class Car {
String model;
int year;
public Car(String model, int year) {
this.model = model;
this.year = year;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Car car = (Car) obj;
return year == car.year && model.equals(car.model);
}
}
public class CompareObjects {
public static void main(String[] args) {
Car car1 = new Car("Tesla", 2020);
Car car2 = new Car("Tesla", 2020);
if (car1.equals(car2))
System.out.println("Both cars are equal.");
else
System.out.println("Cars are not equal.");
}
}
Create object
class Car {
String model;
int year;
public Car(String model, int year) {
this.model = model;
this.year = year;
}
}
public class CreateObject {
public static void main(String[] args) {
Car car = new Car("Toyota", 2021);
System.out.println("Car Model: " + car.model + ", Year: " + car.year);
}
}
Ascii
public class PrintASCII {
public static void main(String[] args) {
char ch = 'A';
int ascii = (int) ch;
System.out.println("ASCII value of " + ch + " is: " + ascii);
}
}
Square root without sqrt
public class SquareRoot {
public static void main(String[] args) {
double number = 25;
double temp;
double sqrt = number / 2;
do {
temp = sqrt;
sqrt = (temp + (number / temp)) / 2;
} while ((temp - sqrt) != 0);
System.out.println("Square root of " + number + " is: " + sqrt);
}
}
Swap two bitwise
public class SwapUsingBitwise {
public static void main(String[] args) {
int a = 5, b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("After swapping: a = " + a + ", b = " + b);
}
}
Gcd
public class GCD {
public static void main(String[] args) {
int num1 = 56, num2 = 98;
while (num1 != num2) {
if (num1 > num2)
num1 -= num2;
else
num2 -= num1;
}
System.out.println("GCD: " + num1);
}
}
Largest of three
public class LargestOfThree {
public static void main(String[] args) {
int a = 10, b = 20, c = 30;
int largest = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
System.out.println("Largest number is: " + largest);
}
}
Smallest of three
public class SmallestOfThree {
public static void main(String[] args) {
int a = 10, b = 5, c = 30;
int smallest = (a < b) ? (a < c ? a : c) : (b < c ? b : c);
System.out.println("Smallest number is: " + smallest);
}
}
Check if a number positive or negative
public class PositiveOrNegative {
public static void main(String[] args) {
int number = -10;
if (number > 0)
System.out.println(number + " is positive.");
else if (number < 0)
System.out.println(number + " is negative.");
else
System.out.println(number + " is zero.");
}
}
Check perfect square
public class PerfectSquare {
public static void main(String[] args) {
int number = 25;
boolean isPerfectSquare = false;
for (int i = 1; i * i <= number; i++) {
if ((number % i == 0) && (number / i == i)) {
isPerfectSquare = true;
break;
}
}
if (isPerfectSquare)
System.out.println(number + " is a perfect square.");
else
System.out.println(number + " is not a perfect square.");
}
}
Display even 1 to 100
public class EvenNumbers {
public static void main(String[] args) {
for (int i = 2; i <= 100; i += 2) {
System.out.print(i + " ");
}
}
}
Display odd
public class OddNumbers {
public static void main(String[] args) {
for (int i = 1; i <= 100; i += 2) {
System.out.print(i + " ");
}
}
}
Sum
public class SumOfNaturalNumbers {
public static void main(String[] args) {
int n = 100;
int sum = n * (n + 1) / 2; // Using the formula
System.out.println("Sum of first " + n + " natural numbers is: " + sum);
}
}
Copy elements from one array to another
public class CopyArray {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = new int[arr1.length];
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
System.out.print("Elements of new array: ");
for (int i : arr2) {
System.out.print(i + " ");
}
}
}
Find frequency of each element
public class FrequencyOfElements {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 4, 4};
int[] freq = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
int count = 1;
if (freq[i] == -1)
continue;
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
count++;
freq[j] = -1;
}
}
freq[i] = count;
}
System.out.println("Element | Frequency");
for (int i = 0; i < freq.length; i++) {
if (freq[i] != -1) {
System.out.println(arr[i] + " | " + freq[i]);
}
}
}
}
Left rotate elements
public class LeftRotateArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int n = 1; // Number of rotations
for (int i = 0; i < n; i++) {
int first = arr[0];
for (int j = 0; j < arr.length - 1; j++) {
arr[j] = arr[j + 1];
}
arr[arr.length - 1] = first;
}
System.out.print("Array after left rotation: ");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
Duplicate elements
public class DuplicateElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4, 5, 4, 5};
System.out.print("Duplicate elements: ");
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
System.out.print(arr[i] + " ");
break;
}
}
}
}
}
Print elements
public class PrintArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.print("Array elements: ");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
Print elements in reverse
public class ReverseArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.print("Array in reverse order: ");
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
Elements in even position
public class EvenPositionElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
System.out.print("Elements at even positions: ");
for (int i = 1; i < arr.length; i += 2) { // Indexing starts from 0
System.out.print(arr[i] + " ");
}
}
}
Elements in odd position
public class OddPositionElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
System.out.print("Elements at odd positions: ");
for (int i = 0; i < arr.length; i += 2) { // Indexing starts from 0
System.out.print(arr[i] + " ");
}
}
}
Largest element
public class LargestElement {
public static void main(String[] args) {
int[] arr = {25, 11, 7, 75, 56};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max)
max = arr[i];
}
System.out.println("Largest element: " + max);
}
}
Smallest element
public class SmallestElement {
public static void main(String[] args) {
int[] arr = {25, 11, 7, 75, 56};
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min)
min = arr[i];
}
System.out.println("Smallest element: " + min);
}
}
Number of elements
public class NumberOfElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println("Number of elements in array: " + arr.length);
}
}
Sum of all items
public class SumOfArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int sum = 0;
for (int i : arr) {
sum += i;
}
System.out.println("Sum of all elements: " + sum);
}
}