Basic java programs
17 NovemberJava program to print floyd’s triangle
In this post, we will see how to print Floyd’s triangle in java. Problem You need to print Floyd’s triangle as below for n=5. Floyd’s triangle **************** 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Floyd’s triangle in java Let’s create class "FloydTriangleMainExample.java" as below [crayon-6902eaff3a537995451933/] When you […]
27 OctoberCount number of words in a string
In this post, we will see how to find number of words in a String. Problem Find the number of words in a String. For example: There are 6 words in below String welcome to java tutorial on Java2blog Algorithm The algorithm will be very simple. Initialize count with 1 as if there are no […]
20 JanuaryHow to check if number is power of two
In this tutorial, we will see how to check if number is power of two. There are many approaches to check if number is power of two or not. Approach 1: It is very easy and straight forward approach. Run a while loop which checks for condition if n is even number (n%2==0). If n is […]
15 DecemberHow to find GCD and LCM of two numbers in java
In this post, we will see how to find Greatest common divisor(GCD) and Least Common Multiple(LCM) in java. GCD is calculated using Euclidean algorithm and LCM is calculated using reduction by GCD Eucid algo for calculating GCD is: Lets say , there are two numbers , a and b so GCD of two numbers = […]
01 AugustJava program to check Armstrong number
Armstrong number is a 3 digit number which is equal to sum of cube of its digits. For example: 371,153 In this post, we will see how to check for Armstrong number in java. [crayon-6902eaff3b82a500880060/] When you run above program, you will get following output. [crayon-6902eaff3b82e572156584/] Please go through Frequently asked java interview Programs for more such […]
01 AugustJava program to reverse a String
In this post, we will see java program to reverse a String. There are many ways to do reverse a String in java, some of them are: Using for loop Declare empty String reverse. This will contain our final reversed string. Iterate over array using for loop from last index to 0th index Add character […]
01 AugustHow to swap two numbers without using temporary variables in java
In this post, we will see how to swap two numbers without using temporary variables. There are three ways to do it. Java program: [crayon-6902eaff3bd34053329082/] When you run above program, you will get following output: [crayon-6902eaff3bd38580100523/] Third way is fastest of all. Please go through Interview programs in java  for more such programs.
10 JulyHow to find prime factors of a number in java
In this post, we will see how to find prime factors of a number in java. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The prime factors of a number are all of the prime numbers that will exactly divide the given number. […]
09 Julyjava program to check prime number
This is easy question. It may not be asked you directly, but you can use this program to create many other complex program. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Program logic: If any number which is not divisible by any other […]