List of logical programs in JavaThe 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 SeriesFibonacci 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. FibonacciWithoutRecursion.java Output ![]() FibonacciWithRecursion.java Output ![]() 2) Armstrong NumberAn 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. = 153 = (1)3+(5)3+(3)3 = 1+125+27 = 153 Let's understand the logic of checking whether a number is Armstrong or not. ArmstrongNumber.java Output ![]() 3) Perfect NumberJust 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. PerfectNumber.java Output ![]() 4) Prime NumberJust 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. PrimeNumber.java Output ![]() 5) Factorial of a numberThe 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: = 8 = 8*7*6*5*4*3*2*1 = 40320 Let's understand the logic of calculating the factorial of a number. FactorialExample.java Output ![]() 6) Reverse a stringThe 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. Let's understand the logic of how to reverse a string by using iteration or StringBuilder/StringBuffer: ReverseString.java Output ![]() 7) Reverse a numberJust 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. ReverseNumber.java Output ![]() 8) Palindrome numberA 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. PalindromeNumber.java Output ![]() 9) Palindrome stringJust 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. PalindromeString.java Output ![]() 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. DuplicateCharacters .java Output ![]()
Next TopicPermGen space Java
|