Posts

Showing posts from February, 2022

Java Program to Reverse a String using Stack - The Coding Shala

Home >> Java Programs >> Reverse a String using Stack  Hey there, welcome back to another post. In this post, we will learn how to reverse a string using a stack in Java. Java Program to Reverse a String using Stack As we know,  Stack data structure follows last in the first out (LIFO), so by using stack we can reverse a given string. For example:  Input: hello output: olleh After storing into stack Stack -> o l l e h Now print stack -> olleh Java Program:  import java.util.Scanner; import java.util.Stack; /** * https://www.thecodingshala.com/ */ public class Main { public static String doReverse(String str) { Stack<Character> stack = new Stack<>(); // push all characters into stack for ( int i = 0; i < str.length(); i++) { stack.push(str.charAt(i)); } // pop characters from stack and build s...

Java Program to Find LCM of Two Numbers - The Coding Shala

Home >> Java Programs >> Find LCM of Two Numbers  Hey there, welcome back to another post. In this post, we will learn how to find the LCM of two numbers in Java. Java Program to Find LCM of Two Numbers The LCM (Least Common Multiple) is the smallest number that can be divided by both numbers. For example, the LCM of 5 and 10 is 10 because 10 is the smallest number that is divisible by both 5 and 10. Example 1:   Input Number1: 8 Number2: 10 Output LCM of 8 and 10 is: 40 Find LCM of Two Numbers using GCD We can find the LCM of two numbers by using GCD. The relation between LCM and GCD is:  a x b = LCM(a, b) * GCD (a, b) LCM(a, b) = (a x b) / GCD(a, b) For example, LCM(8, 10) is: LCM(8, 10) = ( 8 * 10) / GCD(8,10) LCM(8, 10) = 80 / 2 = 40 Java Program:   import java.util.Scanner; /** * https://www.thecodingshala.com/ */ public class Main { public static int findGCD( int num1, int num2) { if (num1 == 0) {...

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala

Home >> Java Programs >> Find GCD of Two Numbers  Hey there, welcome back to another post. In this post, we will learn how to find GCD or HCF of Two Numbers in Java. Java Program to Find GCD or HCF of Two Numbers The GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. For example:  Number1: 6 (2 * 3) Number2: 9 (3 * 3) GCD or HCF of 6 and 9 is: 3 Find GCD of Two Numbers in Java using for Loop As we already know, GCD is the largest number that divides both numbers. We can start a for loop from 1 (because 1 divides all the numbers) to both numbers and if it divides both numbers then update GCD. Java Program:  import java.util.Scanner; /** * https://www.thecodingshala.com/ */ public class Main { public static void findGCD( int num1, int num2) { int gcd = 1; if (num1 == 0) { gcd = num2; } if (num2 == 0) { ...

N-th Tribonacci Number Solution - The Coding Shala

Home >> Programming >> N-th Tribonacci Number  Hey there, welcome back to another post. In this post, we will learn how to solve the N-th Tribonacci Number problem and will implement its solution in Java. N-th Tribonacci Number Problem Statement The Tribonacci sequence Tn is defined as follows: T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0. Given n, return the value of Tn.  Example 1:  Input: n = 4 Output: 4 Explanation: T_3 = 0 + 1 + 1 = 2 T_4 = 1 + 1 + 2 = 4 N-th Tribonacci Number Java Solution using Bottom-Up DP This problem is similar to the Fibonacci series. We can solve it using the Bottom-Up approach of dynamic programming. Time Complexity: O(n) Space Complexity: O(n) The space complexity can be reduced to O(1) by using variables to store the previous three values instead of using an array. Java Program:  class Solution { public int tribonacci( int n) { if (n == 0) return 0; ...

Java Program to Convert Binary to Decimal - The Coding Shala

Home >> Java Programs >> Convert Binary to Decimal  Hey there, welcome back to another post. In this post, we will learn how to convert Binary to Decimal in Java. Java Program to Convert Binary to Decimal You have given a binary number, convert it to a decimal number. Example:  Input: 101 Output: 5 Input: 111 Output: 7 Binary to Decimal Conversion in Java Approach We will the below formula to convert binary to decimal: decimal number = (2^0) * (rightmost digit) + (2^1) * (second rightmost digit) + ...  for example:  binary number = 110 decimal number = 2^0 * 0 + 2^1 * 1 + 2^2 * 1 = 1 * 0 + 2 * 1 + 4 * 1 = 0 + 2 + 4 = 6 Java Program:  import java.util.Scanner; /** * https://www.thecodingshala.com/ */ public class Main { public static void printDecimal( int num) { int decimal = 0; int twos = 1; // 2^0 = 1 initial value while (num > 0) { ...