Floyd Triangle


Note- Floyd Triangle is like
1
2 3
4 5 6
7 8 9 10
------------

import java.util.Scanner;

public class FloydTriangle{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
        int r = in.nextInt();
        int n=0;
        for(int i=0; i<r; i++){
            for(int j=0; j<=i; j++){
                System.out.print(++n+" ");
            }
            System.out.println();
        }
    }
}

Output:

Enter the number of rows which you want in your Floyd Triangle:
5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Verify a number is Even/Odd and Swapping Numbers without using 3rd variable

import java.util.Scanner;
public class EvenOdd{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter a number which you want to check whether that is even or odd");
int n = in.nextInt();
if(n%2==0){
System.out.println(n+" is an even number.");
}else{
System.out.println(n+" is an odd number.");
}
}
}

Output:
Enter a number which you want to check whether that is even or odd
4
4 is an even number.

  

Odd and Swapping Numbers without using 3rd variable


import java.util.Scanner;
public class Swapping{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the 1st number: ");
int x = in.nextInt();
System.out.println("Enter the 2nd number: ");
int y = in.nextInt();
System.out.println("Initial value of x: "+x+" and y: "+y);
x = x+y;
y = x-y;
x = x-y;
System.out.println("After swapping value of x: "+x+" and y: "+y);
}
}

Output:

Enter the 1st number:
43
Enter the 2nd number:
56
Initial value of x: 43 and y: 56
After swapping value of x: 56 and y: 43

Sort Data in Java


Bubble Sort in Java

We can create a java program to sort array elements using bubble sort. Bubble sort algorithm is known as the simplest sorting algorithm.
In bubble sort algorithm, array is traversed from first element to last element. Here, current element is compared with the next element. If current element is greater than the next element,
it is swapped.
  1. public class BubbleSortExample {  
  1.     static void bubbleSort(int[] arr) {  
  1.         int n = arr.length;  
  1.         int temp = 0;  
  1.          for(int i=0; i < n; i++){  
  1.                  for(int j=1; j < (n-i); j++){  
  1.                           if(arr[j-1] > arr[j]){  
  1.                                  //swap elements  
  1.                                  temp = arr[j-1];  
  1.                                  arr[j-1] = arr[j];  
  1.                                  arr[j] = temp;  
  1.                          }  
  1.                           
  1.                  }  
  1.          }  
  1.   
  1.     }  
  1.     public static void main(String[] args) {  
  1.                 int arr[] ={3,60,35,2,45,320,5};  
  1.                  
  1.                 System.out.println("Array Before Bubble Sort");  
  1.                 for(int i=0; i < arr.length; i++){  
  1.                         System.out.print(arr[i] + " ");  
  1.                 }  
  1.                 System.out.println();  
  1.                   
  1.                 bubbleSort(arr);//sorting array elements using bubble sort  
  1.                  
  1.                 System.out.println("Array After Bubble Sort");  
  1.                 for(int i=0; i < arr.length; i++){  
  1.                         System.out.print(arr[i] + " ");  
  1.                 }  
  1.    
  1.         }  
  1. }  
Output:

Array Before Bubble Sort
3 60 35 2 45 320 5 Array After Bubble Sort 2 3 5 35 45 60 320  


Selection Sort in Java


  1. public class SelectionSortExample {  
  2.     public static void selectionSort(int[] arr){  
  3.         for (int i = 0; i < arr.length - 1; i++)  
  4.         {  
  5.             int index = i;  
  6.             for (int j = i + 1; j < arr.length; j++){  
  7.                 if (arr[j] < arr[index]){  
  8.                     index = j;//searching for lowest index  
  9.                 }  
  10.             }  
  11.             int smallerNumber = arr[index];   
  12.             arr[index] = arr[i];  
  13.             arr[i] = smallerNumber;  
  14.         }  
  15.     }  
  16.        
  17.     public static void main(String a[]){  
  18.         int[] arr1 = {9,14,3,2,43,11,58,22};  
  19.         System.out.println("Before Selection Sort");  
  20.         for(int i:arr1){  
  21.             System.out.print(i+" ");  
  22.         }  
  23.         System.out.println();  
  24.           
  25.         selectionSort(arr1);//sorting array using selection sort  
  26.          
  27.         System.out.println("After Selection Sort");  
  28.         for(int i:arr1){  
  29.             System.out.print(i+" ");  
  30.         }  
  31.     }  
  32. }  
Output:

Before Selection Sort 9 14 3 2 43 11 58 22 
After Selection Sort 2 3 9 11 14 22 43 58
N

Armstrong Number in Java


Armstrong Number in Java: Armstrong number is a number that is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.
Let's try to understand why 153 is an Armstrong number.

  1. 153 = (1*1*1)+(5*5*5)+(3*3*3)  
  2. where:  
  3. (1*1*1)=1  
  4. (5*5*5)=125  
  5. (3*3*3)=27  
  6. So:  
  7. 1+125+27=153  

Let's see the java program to check Armstrong Number.
  1. class ArmstrongExample{  
  2.   public static void main(String[] args)  {  
  3.     int c=0,a,temp;  
  4.     int n=153;//It is the number to check armstrong  
  5.     temp=n;  
  6.     while(n>0)  
  7.     {  
  8.     a=n%10;  
  9.     n=n/10;  
  10.     c=c+(a*a*a);  
  11.     }  
  12.     if(temp==c)  
  13.     System.out.println("armstrong number");   
  14.     else  
  15.         System.out.println("Not armstrong number");   
  16.    }  
  17. }  
Output:


armstrong number

Factorial Program in Java


Factorial Program in Java: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:
  1. 4! = 4*3*2*1 = 24  
  2. 5! = 5*4*3*2*1 = 120  
Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek".
The factorial is normally used in Combinations and Permutations (mathematics).
There are many ways to write the factorial program in java language. Let's see the 2 ways to write the factorial program in java.
  • Factorial Program using loop
  • Factorial Program using recursion

Factorial Program using loop in java

Let's see the factorial Program using loop in java.

  1. class FactorialExample{  
  2.  public static void main(String args[]){  
  3.   int i,fact=1;  
  4.   int number=5;//It is the number to calculate factorial    
  5.   for(i=1;i<=number;i++){    
  6.       fact=fact*i;    
  7.   }    
  8.   System.out.println("Factorial of "+number+" is: "+fact);    
  9.  }  
  10. }  
Output:


Factorial of 5 is: 120

Palindrome of Number / Palindrome of String or Reverse a String Program in Java


Palindrome number in java: A palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers.
Palindrome number algorithm
  • Get the number to check for palindrome
  • Hold the number in temporary variable
  • Reverse the number
  • Compare the temporary number with reversed number
  • If both numbers are same, print "palindrome number"
  • Else print "not palindrome number"
Let's see the palindrome program in java. In this java program, we will get a number variable and check whether number is palindrome or not.
  1. class PalindromeExample{  
  1.  public static void main(String args[]){  
  1.   int r,sum=0,temp;    
  1.   int n=454;//It is the number variable to be checked for palindrome  
  1.   
  1.   temp=n;    
  1.   while(n>0){    
  1.    r=n%10;  //getting remainder  
  1.    sum=(sum*10)+r;    
  1.    n=n/10;    
  1.   }    
  1.   if(temp==sum)    
  1.    System.out.println("palindrome number ");    
  1.   else    
  1.    System.out.println("not palindrome");    
  1. }  
  1. }  
Output:

palindrome number


Palindrome of String or reverse a String.


import java.util.Scanner;
public class PalindromeString{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the string which you want to check whether that is palindrome or not: ");
        String s = in.next();
        String r = "";
        for(int i=s.length()-1; i>=0; i--){
            r = r+s.charAt(i);
        }
        System.out.println("Reverse of entered string "+s+" is "+r);
        if(r.equals(s)){
            System.out.println("String "+s+" is palindrome.");
        }else{
            System.out.println("String "+s+" is not palindrome.");
        }
    }
}

Output:

Enter the string which you want to check whether that is palindrome or not:
selenium
Reverse of entered string selenium is muineles
String selenium is not palindrome.

Prime number and prime numbers between a given range program in Java


Prime number in Java: Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.

Let's see the prime number program in java. In this java program, we will take a number variable and check whether the number is prime or not.

  1. class PrimeExample{  
  1.  public static void main(String args[]){  
  1.   int i,m=0,flag=0;    
  1.   int n=17;//it is the number to be checked  
  1.   m=n/2;    
  1.   for(i=2;i<=m;i++){    
  1.    if(n%i==0){    
  1.    System.out.println("Number is not prime");    
  1.    flag=1;    
  1.    break;    
  1.    }    
  1.   }    
  1.   if(flag==0)    
  1.   System.out.println("Number is prime");    
  1. }  
  1. }  
Output:

Number is prime 


How to get the prime numbers between a given range.


package javaTutorial;

import java.util.ArrayList;

import java.util.Scanner;

public class GetPrimeNumbers{

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number from which you want prime number: ");
        int p1 = in.nextInt();
        System.out.println("Enter one more number till which you want prime number: ");
        int p2 = in.nextInt();
        ArrayList<Integer> prime = new ArrayList<Integer>();
        int i=2;
        for(int p=p1; p<=p2; p++){
            i=2;
            for(; i<10; i++){  
                if(p%i==0 && p!=i){
                    break;
                }
            }
            if(i==10){
                prime.add(p);
            }
        }
        System.out.println("Prime numbers between "+p1+" and "+p2+" are: ");
        for(int j=0; j<prime.size(); j++){
            System.out.print(prime.get(j).toString()+", ");
        }      
    }
}
 
Output:
 
Enter a number from which you want prime number:
10
Enter one more number till which you want prime number:
30
Prime numbers between 10 and 30 are:
11, 13, 17, 19, 23, 29,

Fibonacci series in Java


In fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of fibonacci series are 0 and 1.
There are two ways to write the fibonacci series program in java:
  • Fibonacci Series without using recursion
  • Fibonacci Series using recursion
Fibonacci Series in Java without using recursion

Let's see the fibonacci series program in java without using recursion.
  1. class FibonacciExample1{  
  2. public static void main(String args[])  
  3. {    
  4.  int n1=0,n2=1,n3,i,count=10;    
  5.  System.out.print(n1+" "+n2);//printing 0 and 1    
  6.     
  7.  for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed    
  8.  {    
  9.   n3=n1+n2;    
  10.   System.out.print(" "+n3);    
  11.   n1=n2;    
  12.   n2=n3;    
  13.  }    
  14.   
  15. }}  
Output:

0 1 1 2 3 5 8 13 21 34

Floyd Triangle

Note- Floyd Triangle is like 1 2 3 4 5 6 7 8 9 10 ------------ import java.util.Scanner; public class FloydTria...

Popular Post