Posts

Showing posts from January, 2022

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 printBinary( int num) {

Find Second Smallest Element in the Array - The Coding Shala

Home >> Programming >> Find Second Smallest Element in the Array  Hey there, welcome back to another post. In this post, we will learn how to find the second smallest number in the array in Java. Find Second Smallest Element in the Array Problem Statement You have given an integer array, return the second smallest element from the array if exists else return no second smallest element exists. Example 1:  Input: [1, 4, 2, 7, 90, -1, -4] Output: -1 Find Second Smallest Element in the Array Solution using Single Iteration Approach By using the below steps we can find the second smallest element in the array by using a single iteration: Initialize two variables first and second for smallest and second smallest numbers and the initial value will be Integer.MAX_VALUE. Traverse the array and check if the current element is smaller than the smallest element then update both first and second. If the current element is between first and second then only update the seco

Find Second Largest Element in Array - The Coding Shala

Home >> Programming >> Find Second Largest Element  Hey there, welcome back to another post. In this post, we will learn how to find the second largest element in the array in Java. Find Second Largest Element in Array Find the second largest element in the given array if exist else return -1. Example 1:  Input: [1, 6, 2, 7, 10, 8, -11] Output: 8 By using the below methods we can find the second largest element in the array. Find Second Largest Element Java Solution Using Single Iteration We can find the largest and second-largest elements in the array by using a single iteration. The approach is below. Approach / Explanation If the given array size if less than 2 then there is no second largest element so return -1. Create two variables first and second to store the largest and second-largest elements and the initial value is Integer.MIN_VALUE. Traverse the array and check if the current element is greater than the largest element then update both the first an

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}; System.out.println( &q

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: &q

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:

Java Program to Convert String to int - The Coding Shala

Home >> Java Programs >> Convert String to int  Hey there, welcome back to another post. In this post, we are going to learn how to convert String to int in Java. Java Program to Convert String to int There are a few ways by using those we can convert String to int data type in Java. The most common ways are below: By using Integer class's parseInt() method. By using Integer class's valueOf() method. Now let's see these methods with examples in Java. Java Example to Convert String to int using parseInt() method We can convert a String to int by using the Integer wrapper class's parseInt() method.  Java Program:  /** * https://www.thecodingshala.com/ */ public class Main { public static void main(String[] args) { String str = "100" ; // using Integer.parseInt() int num1 = Integer.parseInt(str); System.out.println( "Converted int, num1 is: " + num1); System.out.println

Java Program to Convert int to String - The Coding Shala

Home >> Java Programs >> Convert int to String  In this post, we will learn how to convert int to string in Java. Java Program to Convert int to String There are a few ways by using those we can convert int data type to String data type in Java. The most common ways are below: By using String.valueOf() method. By using Integer.toString() method. By using String.format() method. Now let's see these methods one by one with examples in Java. Java Program:  /** * https://www.thecodingshala.com/ */ public class Main { public static void main(String[] args) { int num1 = 100; // using String.valueOf(int) String str1 = String.valueOf(num1); System.out.println( "Converted String str1 is: " + str1); System.out.println( "5 + int number is: " + (5 + num1)); System.out.println( "5 + str1 is: " + (5 + str1)); // using Integer.toString(int) String str2 = In