Skip to content

Instantly share code, notes, and snippets.

View ravindugm's full-sized avatar
🏡
Working from home

Ravindu Miyuranga ravindugm

🏡
Working from home
View GitHub Profile
@ravindugm
ravindugm / BasicJava.txt
Created June 30, 2021 09:01
Java programming practice codes.
Q1
CMJD81 :/> notepad Example.java
//-----------------------------------------------------------------------
class Example{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
//-----------------------------------------------------------------------
CMJD81 :/> javac Example.java
@ravindugm
ravindugm / Elevator.java
Created June 4, 2023 16:21
Elevator algorithm in Java
public class Main {
public static void main(String[] args) {
moveToFloor(1);
moveToFloor(2);
moveToFloor(7);
}
private static int currentFloor = 1;
public static void moveToFloor(int destinationFloor) {
@ravindugm
ravindugm / Compare.java
Created August 15, 2023 09:37
Compare string arrays using Java stream
import java.util.Arrays;
public class Compare {
public static void main(String[] args) {
String arrayOne[] = {"Test1", "Test2"};
String arrayTwo[] = {"Test2", "Test1", "Test3"};
boolean arrayEquals = false;
if (Arrays.stream(arrayOne).count() == Arrays.stream(arrayTwo).count()) {
@ravindugm
ravindugm / ReversedString.java
Created September 5, 2023 13:30
Reverse a string without using toCharArray method
public class Main {
public static void main(String[] args) {
String input = "GitHub Gist Page";
char[] stringArray = new char[input.length()];
for (int i = 0; i < stringArray.length; i++) {
stringArray[i] = input.charAt(i);
}
@ravindugm
ravindugm / Fibonacci.java
Last active December 13, 2023 11:23
Fibonacci Series without using recursion
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
public class Main {
public static void main(String[] args) {
int n = 15; // You can change this to the number of Fibonacci numbers you want
int first = 0;
int second = 1;
System.out.print(first + ", " + second);
@ravindugm
ravindugm / Palindrome.java
Created November 19, 2023 20:27
Check if given number is a palindrome
public class Main {
public static void main(String[] args) {
int input = -121;
System.out.println("Is palindrome " + isPalindrome(input));
}
public static boolean isPalindrome(int num) {
if (num < 0) {
@ravindugm
ravindugm / Palindrome.java
Created November 20, 2023 06:44
Check if given string is a palindrome
public class Main {
public static void main(String[] args) {
String input = "RMR";
System.out.println("Is palindrome " + isPalindrome(input));
}
public static boolean isPalindrome(String input) {
String reverseStr = "";
@ravindugm
ravindugm / RomanToInt.java
Created November 21, 2023 05:11
Given an integer, convert it to a roman numeral
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// Test cases
System.out.println(romanToInt("III")); // Output: 3
System.out.println(romanToInt("LVIII")); // Output: 58
System.out.println(romanToInt("MCMXCIV")); // Output: 1994
@ravindugm
ravindugm / Palindrome.java
Created November 21, 2023 10:49
Valid Palindrome
/*
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
Example 1:
Input: s = "A man, a plan, a canal: Panama"
@ravindugm
ravindugm / PalindromePermutation.java
Created November 24, 2023 01:51
Palindrome permutation
/*
Given a string, determine if a permutation of the string could form a palindrome.
Example 1:
Input: "code"
Output: false
Example 2:
Input: "aab"