List of logical programs in Java
The programming paradigm that is mostly based on formal logic is referred to as Logical Programming. The logical Java programs are mostly asked by the interviewers like Fibonacci series, Armstrong Number, Prime Number, and Perfect Number, etc. The Logical programs are designed by using certain logic and can say 70 percent code of the program is a set of logic.
There can be the following logical programs which are mostly asked:
Let's understand each of the above logical programs one by one.
1) Fibonacci Series
Fibonacci series is a special type of series in which the next term is the sum of the previous two terms. For example, if 0 and 1 are the two previous terms in a series, then the next term will be 1(0+1).
In Java, we can make the Fibonacci series program either by using the recursion or without recursion. Let's understand the program of the Fibonacci series by using recursion or without recursion one by one.
2) Armstrong Number
An Armstrong Number is a special positive number whose sum of cubes of its digit is equal to that number. The number 154 is an Armstrong number because if we perform the sum of cubes of each digit, it will result in the same number.
Let's understand the logic of checking whether a number is Armstrong or not.
3) Perfect Number
Just like the Armstrong number, the Perfect Number is also a special type of positive number. When the number is equal to the sum of its positive divisors excluding the number, it is called a Perfect Number. For example, 28 is the perfect number because when we sum the divisors of 28, it will result in the same number. The divisors of 28 are 1, 2, 4, 7, and 14. So,
28 = 1+2+4+7
28 = 28
Let's understand the logic of checking whether a number is Perfect or not.
4) Prime Number
Just like the Perfect and Armstrong number, Prime number is also a special type of number. When the number is divided greater than 1 and divided by 1 or itself is referred to as the Prime number. 0 and 1 are not counted as prime numbers. All the even numbers can be divided by 2, so 2 is the only even prime minister.
Let's understand the logic of checking whether a number is prime or not.
5) Factorial of a number
The product of all positive integers which are less than or equal to the given positive number is referred to as the factorial of that given integer number. For example, the factorial of the number 8 can be calculated by multiplying all the integers which are less than or equal to the 8 like this:
Let's understand the logic of calculating the factorial of a number.
6) Reverse a string
The reverse of a string is another logical program that is mostly asked by the interviewers. We can reverse a string either by using the StringBuilder/StringBuffer or by reverse iteration.
7) Reverse a number
Just like String, a program of reversing a number is also frequently asked by the interviewers. In Java, we can reverse a number either by using for loop, while loop, or using recursion. The simplest way to reverse a number is by using for loop or while loop. In order to reverse a number, we have to follow the following steps:
Let's understand the logic of reversing a number by using the loop.
8) Palindrome number
A palindrome number is a special number. After reversing a number, if it is the same as the original number referred to as a palindrome number. There can be a series of palindrome numbers. For example, 121, 232, 34543, 171, and 343 etc.
Let's understand the logic of checking whether a number is a palindrome or not.
9) Palindrome string
Just like a number, a string can also be a palindrome. The logic of checking a string is different from the number.
Let's understand the logic of checking string palindrome.
10) Find duplicate characters in a string.
Finding duplicate characters in a string is another logical program that is also frequently asked by the interviewers. We can find the duplicate characters in a string and the occurrence of that character in a string.
The logic of this program is a little tricky because there are two processes, i.e., finding the duplicate characters and occurrence of that character.
Let's understand the logic of finding the duplicate characters in a string.