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

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