If you have been doing Java programming then you may know that the use of == operator is not the correct way to compare floating point values in Java. If you use equality operator to compare float and double variables then it can cause an endless loop in Java, but is there a way to prevent that loop from running infinitely? Yes, instead of using == operator, you can use relational operator e.g. less than (<) or greater than (>) to compare float and double values.
Showing posts with label core java. Show all posts
Showing posts with label core java. Show all posts
Saturday, 15 January 2022
Friday, 3 June 2016
Java Program to Reverse an Integer Number without using String - Example
When I was asked "Can you write a program to reverse a number in Java" long long years back, I did a trick. I converted the number to String by just concatentating with empty String e.g. "" + 123 and then converted that Stirng to StringBuilder can caled the reverse() method.
That solved the problem but Interviewer was not satisfied. He said Ok, but then asked again, can you reverse a number without using anything String or StringBuilder and by building your logic with basic programming elements. I was confused, what do you mean by basic programming elements? He said, I can use loop, if block, operator, and other basic stuff which makes a programming language.
Saturday, 28 May 2016
How to Check if Given Number is Prime Number in Java
Before writing a progarm to check if a number is prime or not, let's first revise what is a prime number? In Maths, a number is said to be prime if it is not divisible by any number other than itself. You can now convert this definition to code and create your Java program to check if given number is prime or not.
Friday, 27 May 2016
How to Calculate Factorial in Java using Recursion and Loop
You can calculate factorial of a given number in Java program either by using recursion or loop. The factorial of a number is equal to number*fact(number-1), which means its a recursive algorithm and that's why it is often used to teach recursion to programmers in computer programming classes.
But, recursion is not always preffered in production code and that's why you also need to learn how to convert a recursive algorithm to an iterative algorithm i.e. algorithm which make use of various loop construct in Java programming language.
Here in this Java beginners tutorial, I'll teach you both ways to calcualte factorial in Java i.e. with and without recursion. Since recursive algorithm is very simple, we'll fisrt look at that and later we'll check out how to calculate factorial without recursion.
But, recursion is not always preffered in production code and that's why you also need to learn how to convert a recursive algorithm to an iterative algorithm i.e. algorithm which make use of various loop construct in Java programming language.
Here in this Java beginners tutorial, I'll teach you both ways to calcualte factorial in Java i.e. with and without recursion. Since recursive algorithm is very simple, we'll fisrt look at that and later we'll check out how to calculate factorial without recursion.
Subscribe to:
Posts (Atom)


