Basic java programs
- 17 November
Java 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-6729f7450876f643524902/] When you […]
- 27 October
Count 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 January
How 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 December
How 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 August
Java 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-6729f7450bb8b099179692/] When you run above program, you will get following output. [crayon-6729f7450bb91445505396/] Please go through Frequently asked java interview Programs for more such […]
- 01 August
Java 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 August
How 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-6729f7450ce8a092980756/] When you run above program, you will get following output: [crayon-6729f7450ce93620179588/] Third way is fastest of all. Please go through Interview programs in java  for more such programs.
- 10 July
How 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 July
java 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 […]