What is a prime factor A prime factor is made up of two terms : Factor and Prime Number. Prime Factor  = Factor + Prime Number A factor of a number is a number which completely divides the number. By completely divides, it means that the remainder of division is zero. A prime number is a number which is only divisible by itself and 1. Examples of prime numbers are 2, 3, 5, 7, 11, 17, 29 etc. Logic Prime factors of a number can be calculated using the following steps: Iterate over a loop using a counter starting from 2(since it is the smallestRead More →

What is Fibonacci Series Fibonacci Series is a sequence of numbers in which each number(starting from the third number) is a sum of previous two numbers in the series and the first 2 elements of the series are 0 and 1. Thus the series becomes, 0, 1, 1, 2, 3, 5, 8, 13, 21 and so on…. Problem Given a number as input, determine whether it belongs to the Fibonacci series or not. Example, input numbers such as 0, 5, 34, 55 belong to Fibonacci series but numbers such as 4, 9, 20, 52 do not belong to it. We need to write a javaRead More →

Problem Suppose there is a bunch of String objects and you want to join them by a separator to form a combined string. For Example, I have 3 strings “www”, “codippa” and “com”. I want to join these strings with a “.”(dot). Normal approach would be : String s1 = “www”; String s2 = “codippa”; String s3 = “com”; String joinedString = s1 + “.” + s2 + “.” + s3; This approach is fine but only when the number of strings to be joined is less. When the string values to be joined are more, this approach has following shortcomings: Code will be moreRead More →

Problem Suppose there are 2 strings and their values need to be interchanged. String variables are declared as String stringOne = “This is “; String stringTwo = “codippa.com “; and the requirement is to interchange(or swap) their values such that their values become stringOne = “codippa.com “; stringTwo = “This is “; A typical approach to interchange the values of these string variables is by using a third variable as String temp = “”; // store value of first string in temp temp = stringOne; // store the value of second string into first string stringOne = stringTwo; // store the value stored in tempRead More →

A string is said to be Palindrome which when reversed is the same as original string. In other words, when a string and its reversed form are same, the string is called a Palindrome string. Example, “radar”, “rececar”, “madam”, “refer” are all palindrome strings. There are many ways to check for a Palindrome string in java. This post will focus on testing for Palindrome string using a java.util.Stack. A stack is a data structure which holds its elements in Last In First Out (LIFO) principle. Element added to the top of the stack is retrieved first from it and element added first is retrieved last.Read More →

Sorting means arranging elements in a specific order. Order can be of two types : ascending and descending. Ascending order is when the elements are arranged in order of increasing value(smaller to larger). Descending order implies the elements are arranged in order of decreasing value(or larger to smaller values). Sorting a one dimensional array in java is straight forward and easy, just call java.util.Arrays.sort with the array to sort as argument and you get the array sorted. But what if you want to sort a two dimensional array. There is no direct method to sort a two dimensional array in java. Just pause and think,Read More →

A fraction is a quantity which is not a whole number rather it is formed by using two numbers. A fraction has two parts : numerator and denominator separated by a “/”. Examples of fraction are : 15/2, 35/6 A fraction should always be represented in reduced(or simplest) form where there are no common multiples of numerator and denominator. Example, 56/12 is not in its reduced from since its numerator and denominator still have common multiples. It reduced form will be 14/3(dividing both numerator and denominator by 4). For reducing a fraction to its simplest form, Greatest Common Divisor(GCD) of its numerator and denominator isRead More →

Many times it happens when you are iterating over an ArrayList and you want to remove items from this list based on some condition. For Example, there is a list of students and you want to remove those students whose result is “Fail”. Code for this requirement is as simple as below :Read More →