Posts

Showing posts with the label Java Program

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) { ...

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) { ...

Java Program to Convert Decimal to Binary - The Coding Shala

Home >> Java Programs >> Convert Decimal to Binary  Hey there, welcome back to another post. In this post, we will learn how to convert decimal to binary in Java. Java Program to Convert Decimal to Binary You have given a decimal number. Write a Java program to convert decimal to binary. Example 1:  Input: 7 Output: 111 Input: 10 Output: 1010 Decimal to Binary Conversion using Array Approach To convert decimal to binary we can follow the below steps: Store the remainder in the array when we divide the number by 2. Divide the number by 2. Follow the above two steps until the number is above 0. Print the array in reverse order. For example:  4 % 2 = 0 => 4/2 = 2 2 % 2 = 0 => 2/2 = 1 1 % 2 = 1 => 1/2 = 0 Binary of 4 is: 100 Java Program:  import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * https://www.thecodingshala.com/ */ public class Main { public static void printBina...

Java Program to Check if Two Strings are Anagram - The Coding Shala

Home >> Java Programs >> Check if Two Strings are Anagram  Hey there, welcome back to another post. In this post, we will learn how to check if two strings are anagram or not in Java. Java Program to Check if Two Strings are Anagram Two strings are called anagrams if one string can be converted to another string by rearranging the characters of the first string. For example: String a = "race" and String b = "care" are anagrams, because from string "race" we can make the string "care". By using the below ways, we can check if two strings are anagram or not: Convert strings to char arrays then sort the arrays. Java Example to Check if Two Strings are Anagram using Sorting Strings are anagram if the below conditions are true: The length of both strings should be the same. Characters and their frequencies should be the same in both the string. We can convert both the strings to char array. After sorting the arrays we can check if ...

Java Program to Check if Array Contains a Given Value - The Coding Shala

Home >> Java Programs >> Check if Array Contains a Given Value  Hey there, welcome back to another post. In this post, we will learn how to check if the array contains a given value or not in Java. Java Program to Check if Array Contains a Given Value Write a Java program to check if an array contains a given value or not.  Example 1:  Input Array: [1, 5, 7, 3, 9] Given value: 7 Output: 7 is present in the array We can check this using the below ways:  Using a loop. using List.contains() method. Let's see the above methods using Java Examples. Java Example to Check if Array Contains a Given Value using For Loop We can use a for loop to iterate the array and will check if the given value is available or not in the array. Java Program:  /** * https://www.thecodingshala.com/ */ public class Main { public static void main(String[] args) { // integer array int [] arr = {4, 3, 2, 11, 90, -7, 45, 2}; Sy...

Java Program to Display Factors of a Number - The Coding Shala

Home >> Java Programs >> Display Factors of a Number  Hey there, welcome back to another post. In this post, we are going to learn how to find all the factors of a given natural number in Java. Java Program to Display Factors of a Number First, let's understand what are the factors or divisors. If a number1 divides the number2 completely without any reminders, then number1 is a factor of number2. For example: The factor of 10 is 1, 2, 5, 10 because all these numbers divide 10 without any reminders. Java Program to Print Factors of a Number using for Loop Using the for loop from 1 to the number itself, we can print all the factors. Java Program:   import java.util.Scanner; /** * https://www.thecodingshala.com/ */ public class Main { public static void printFactors( int num) { System.out.println( "Factors of " + num + " are: " ); for ( int i = 1; i <= num; i++) { if (num % i == 0) { ...

Java Program to Convert int to double - The Coding Shala

Home >> Java Programs >> Convert int to double  Hey there, welcome back to another post. In this post, we are going to learn how to convert int to double in Java. Java Program to Convert int to double As we know, double is a larger data type than int so the Java compiler automatically converts int to double when we assign an int to double data type. The most common ways are below to convert int to double: Implicit type conversion. By using the Double Wrapper class's valueOf() method. Now let's see the above methods in detail with examples in Java. Java Example to Convert int to double by Implicit Conversion The Java compiler will automatically convert smaller data types to larger data types. Java Program:  /** * https://www.thecodingshala.com/ */ public class Main { public static void main(String[] args) { int num = 50; // implicit type conversion double dNum = num; System.out.println( "int is: ...

Java Program to Convert double to int - The Coding Shala

Home >> Java Programs >> Convert double to int Hey there, welcome back to another post. In this post, we are going to learn how to convert double to int in Java. Java Program to Convert double to int The most common ways to convert double to int in Java are below:  By using type-casting. By using Double class's intValue() method. By using Math class's round() method. Now let's see those methods with examples in Java. Java Example to Convert double to int by using Type Casting We can use explicit type casting to convert double to int data type, it will truncate all the digits after the decimal. Java Program:  /** * https://www.thecodingshala.com/ */ public class Main { public static void main(String[] args) { double d1 = 5.70; // type casting int num1 = ( int ) d1; System.out.println( "double is: " + d1); System.out.println( "Converted int is: " + num1); } } Output:...