Blog

Program Logics in Java

  • (4.0)
  • | 925 Ratings
Program Logics in Java

Some Important Program Logics in Java

Fibonacci Series

Fibonacci series is one of the most famous mathematical formulas.  In Fibonacci series, each number is a sum of two preceding numbers to it. Let's consider an example here:

0,1,1,2,3,5,8,13,21,34,55 and so on the sequence continues. 

import java.util.Scanner;
public class Fibonacci {
     
      static void showfibonacci(int no){
            int f1, f2=0, f3=1;
            for(int i=1;i <=no;i++){
                  System.out.println(f3);
                  f1 = f2;
                  f2 = f3;
                  f3 = f1 + f2;
            }
      }     
 
      public static void main(String args[]){
            Scanner s = new Scanner(System.in);
            System.out.println("Enter Number: ");
            int n = s.nextInt();
 
            if(n > 0){
                  showfibonacci(n);
            }else{
                  System.out.println("Please enter positive number");
            }
      }
 
}

Output:

Enter Number: 10
1
1
2
3
5
8
13
21
34
55

Prime Number

The prime number in  Java: prime numbers are a type of numbers in java which is equal to one or greater than 1. A number is considered as a prime when it is divided by 1 or itself. For e.g  2, 3, 5, 7, 11, 13, 17 etc.   

import java.util.Scanner;
public class PrimeNum{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        System.out.println("Enter a number : ");
        int num = s.nextInt();
        int i=2;
        for(; i<10; i++){
            if(num%i==0 && num!=i){
                System.out.println(num+" is not a prime number.");
                break;
            }
        }
            if(i==10){
                    System.out.println(num+" is a prime number.");
            }
    }   
}

Output:

Enter a number : 17
17 is a prime number.

Armstrong Number

import java.util.Scanner;
 
public class ArmstrongNumber{
    
public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter a number: ");
    int num = s.nextInt();
    int a = num, r=0, result=0;
 
    while(a!=0){
    r = a%10;
    a = a/10;
    result = result + r*r*r;
    }
    if(result==num){
        System.out.println(num+" is an armstrong number.");
    }
    else{
        System.out.println(num+" is not an armstrong number.");
    }
    }
}

Output:

Enter a number: 407
407 is an armstrong number.

Perfect Number

A perfect number is a positive integer that is equal to the sum of its proper positive divisors.
Ex. 6 = (1 + 2 + 3) or It is a programming method in java to check whether a given number is perfect or not. A perfect number is a positive integer which is equal to the sum of its positive divisors except for the number itself.

public class PerfectNum {

    public boolean checkPerfectNum(int n){

        int t = 0;
        for(int i=1;i<=n/2;i++){
            if(n%i == 0){
                t += i;
            }
        }
        if(t == n){
            System.out.println(n + " is a perfect number");
            return true;
        } else {
            System.out.println(n + " is not a perfect number");
            return false;
        }
    }

    public static void main(String a[]){
        PerfectNum obj = new PerfectNum();
        obj.checkPerfectNum(82);
    }
}

Output:

82 is not a perfect number

Factorial

import java.util.Scanner;

public class Factorial{

public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the number: ");
    int num = in.nextInt();
    int fact =1;
    for(int i=num; i>0; i--){
    fact = fact*i;
    }
    System.out.println("Factorial of "+num+" is "+fact);
    }
}

Output:

Enter the number: 7
Factorial of 7 is 5040

Reverse a String/Palindrome String

It is a programming method in java to check whether a string is a palindrome or not. A string is declared as a palindrome if it is unchanged even when you reverse it. For example ‘dad’ is a palindrome string because it will remain unchanged when you write it reversely. When it comes to our programming part whenever a user input a string, it will automatically be reversed, and it will be compared with the input string.

import java.util.Scanner;

public class Palindrome{

    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the string: ");
        String str = s.next();
        String result = "";
        for(int i=str.length()-1; i>=0; i--){
            result = result+str.charAt(i);
        }
        System.out.println("Reverse of "+str+" is "+result);
        if(result.equals(str)){
            System.out.println(str+" is palindrome.");
        }
        else{
            System.out.println(str+" is not palindrome.");
        }
    }
}

Output:

Enter the string: Java
Reverse of Java is avaJ
Java is not palindrome.

Reverse a Number/Palindrome Number

A palindrome number is a number in which the number is unchanged even if you reverse it. Let's consider an example here: ‘12321’ is considered as a palindrome number because it will be the same number even if you write it reversely.

import java.util.Scanner;
public class PalindromeNum {
      static int checkpalindrome(int num){
            int newN = 0, rem, temp;
            temp = num;
            //find sum of all digit's of the number.
            while(temp != 0){
                  rem = temp % 10;
                  newN = newN*10 + rem;
                  temp = temp/10;
            }
            //Check if sum of all digit's of the number
            //is equal to the given number or not.
            if(newN == num){
                  System.out.println(num +" is palindrome.");
            }else{
                  System.out.println(num +" is not palindrome.");
            }
            return newN;
      }    

      public static void main(String args[]){
             Scanner s = new Scanner(System.in);
            System.out.println("Enter the Number: ");
            int no = s.nextInt();

            int n = checkpalindrome(no);
            System.out.println("Reverse of a number :" +n);
      }
}

Output:

Enter the Number: 214412
214412 is palindrome.
Reverse of a number :214412

Swap number without using 3rd variable

import java.util.Scanner;

public class SwapNum{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the 1st number: ");
        int i = s.nextInt();
        System.out.println("Enter the 2nd number: ");
        int j = s.nextInt();

        System.out.println("Original value of i: "+i+" and j: "+j);
        i = i+j;
        j = i-j;
        i = i-j;
        System.out.println("After swapping value of i: "+i+" and j: "+j);
    }
}

Output:

Enter the 1st number: 7
Enter the 2nd number: 8
Original value of i: 7 and j: 8
After swapping value of i: 8 and j: 7

Bubble Sort

Bubble sort is used to compare pairs of adjacent elements and swaps their position if they are in the wrong order. It is used in computer science to sort the elements. 

public class BubbleSort {  
    static void bubblesorting(int[] a) {  
        int n = a.length;  
        int temp = 0;  
         for(int i=0; i < n; i++){  
                 for(int j=1; j < (n-i); j++){  
                          if(a[j-1] > a[j]){  
                                 //swap elements  
                                 temp = a[j-1];  
                                 a[j-1] = a[j];  
                                 a[j] = temp;  
                         }  
                          
                 }  
         }  
  
    }  
    public static void main(String[] args) {  
                int a[] ={2,30,55,22,5,547,97};  
                 
                System.out.println("Before Bubble Sort");  
                for(int i=0; i < a.length; i++){  
                        System.out.print(a[i] + " ");  
                }  
                System.out.println();  
                  
                bubblesorting(a);
                 
                System.out.println("After Bubble Sort");  
                for(int i=0; i < a.length; i++){  
                        System.out.print(a[i] + " ");  
                }  
   
        }  
}

Output:

Before Bubble Sort
2 30 55 22 5 547 97 
After Bubble Sort
2 5 22 30 55 97 547

Selection Sort

public class SelectionSort {  
    static void selectionsorting(int[] a) {  
         for (int i = 0; i < a.length - 1; i++)  
        {  
            int k = i;  
            for (int j = i + 1; j < a.length; j++){  
                if (a[j] < a[k]){  
                    k = j;
                }  
            }  
            int smallNum = a[k];   
            a[k] = a[i];  
            a[i] = smallNum;  
        }  
  
    }  
    public static void main(String[] args) {  
                int a[] ={2,30,55,22,5,547,97};  
                 
                System.out.println("Before Selction Sort");  
                for(int i=0; i < a.length; i++){  
                        System.out.print(a[i] + " ");  
                }  
                System.out.println();  
                  
                selectionsorting(a);
                 
                System.out.println("After Selection Sort");  
                for(int i=0; i < a.length; i++){  
                        System.out.print(a[i] + " ");  
                }  
   
        }  
}

Output:

Before Selction Sort
2 30 55 22 5 547 97 
After Selection Sort
2 5 22 30 55 97 547

Insertion Sort

public class InsertionSort {  
    static void insertionsorting(int[] a) {  
          int n = a.length;  
        for (int j = 1; j < n; j++) {  
            int key = a[j];  
            int i = j-1;  
            while ( (i > -1) && ( a [i] > key ) ) {  
                a [i+1] = a [i];  
                i--;  
            }  
            a[i+1] = key;  
        }    
    }  
    public static void main(String[] args) {  
                int a[] ={2,30,55,22,5,547,97};  
                 
                System.out.println("Before Insertion Sort");  
                for(int i=0; i < a.length; i++){  
                        System.out.print(a[i] + " ");  
                }  
                System.out.println();  
                  
                insertionsorting(a);
                 
                System.out.println("After Insertion Sort");  
                for(int i=0; i < a.length; i++){  
                        System.out.print(a[i] + " ");  
                }  
   
        }  
}

Output:

Before Insertion Sort
2 30 55 22 5 547 97 
After Insertion Sort
2 5 22 30 55 97 547

Remove duplicate elements from an Array

import java.util.Arrays; 
public class RemoveDuplicate{  
    public static int removeDuplicateNum(int a[], int n){  
         if (n==0 || n==1){  
            return n;  
        }  
        int[] temp = new int[n];  
        int j = 0;  
        for (int i=0; <n-1; for="" i="0;" if="" int="" length="a.length;" main="" pre="" public="" return="" static="" void="">
</n-1;>

Output:

5 15 27 30 50 55 87

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Ravindra Savaram
About The Author

Ravindra Savaram is a Content Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.


DMCA.com Protection Status

Close
Close