Javatpoint Logo
Javatpoint Logo

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:

List of logical programs in Java
  1. Fibonacci Series.
  2. Armstrong Number.
  3. Perfect number.
  4. Prime number.
  5. Factorial of a number.
  6. Reverse a string.
  7. Reverse a number.
  8. Palindrome number
  9. Palindrome string.
  10. Find duplicate characters in a string.

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.

FibonacciWithoutRecursion.java

Output

List of logical programs in Java

FibonacciWithRecursion.java

Output

List of logical programs in Java

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.

= 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

List of logical programs in Java

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.

PerfectNumber.java

Output

List of logical programs in Java

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.

PrimeNumber.java

Output

List of logical programs in Java

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:

= 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

List of logical programs in Java

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.

Let's understand the logic of how to reverse a string by using iteration or StringBuilder/StringBuffer:

ReverseString.java

Output

List of logical programs in Java

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:

  • We need to calculate the remainder of the number using the modulo
  • After that, we need to multiply the variable reverse by 10 and add the remainder into it.
  • We then divide the number by 10 and repeat steps until the number becomes 0.

Let's understand the logic of reversing a number by using the loop.

ReverseNumber.java

Output

List of logical programs in Java

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.

PalindromeNumber.java

Output

List of logical programs in Java

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.

PalindromeString.java

Output

List of logical programs in Java

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

List of logical programs in Java
Next TopicPermGen space Java




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA