Program to print small case alphabets in pyramid shape

public class Pyramid_Alpha {
public static void main(String[] args)
 {
 int n=3;
 for (int r = 0; r < n; r++)
 {
 char a=97;
 for (int s = 0; s < n-1-r; s++) 
 {
 System.out.print(" ");
 }
 for (int st = 0; st <=2*r; st++) 
 {
 System.out.print(a++ +" ");
 }
 System.out.println();
 }
}
}

Outpur:-

    a 
  a b c 
a b c d e

Program to print Pyramid Down

public class Pyramid{
public static void main(String[] args) {
 int n = 3;
 for (int r = n-1 ; r >= 0; r--) 
 {
 for (int s = 0; s < n-1-r; s++) 
 {
 System.out.print(" ");
 }
 for (int st = 0; 2*r >= st; st++) 
 {
 System.out.print("* "); 
 }
 System.out.println();
 }
 }
}

Output:-

* * * * * 
  * * * 
    *

Program to print Pyramid Left

public class Pyramid{
public static void main(String[] args) {
int n=3;
 for (int row=0;row<n-1 ;row++ )
 {
 for (int col=0;col<n;col++)
 {
 if (row+col>=n-1)
 {
 System.out.print("* ");
 }
 else
 {
 System.out.print(" ");
 }
 }
 System.out.println();
 }
 for (int row=0;row<n ;row++ )
 {
 for (int col=0;col<n;col++)
 {
 if (col>=row)
 {
 System.out.print("* ");
 }
 else
 {
 System.out.print(" ");
 }
 }
 System.out.println();
 }
 }
}

Output:-

       *
    *  *
 *  *  *
    *  *
       *

Program to print Pyramid Rhombus

public class Pyramid{
public static void main(String[] args) {
 int n = 5;
 for (int rows = 0; rows < n-2; rows++) {
 for (int col = 0; col < n; col++) {
 if (2*rows + col >= n-1) {
 System.out.print("* ");
 } 
 else {
 System.out.print(" ");
 } 
 }
 System.out.println();
 }
 for (int rows = 1; rows < n; rows++) {
 for (int col = 0; col < n; col++) {
 if (2*rows <= col) {
 System.out.print("* ");
 } 
 else {
 System.out.print(" ");
 }
 }
 System.out.println();
 }
 }
}

 

Output:-

     *
   * * *
 * * * * *
   * * *
     *

Program to print pyramid up

public class Pyramid
{
 public static void main(String[] args)
 {
 int n = 3;
 for (int r = 0; r < n; r++) 
 {
 for (int s = 0; s < n-1-r; s++) 
 {
 System.out.print(" ");
 }
 for (int st = 0; 2*r >= st; st++) 
 {
 System.out.print("* "); 
 }
 System.out.println();
 }
 }
}

Output:-

     *
   * * *
 * * * * *

Program to perform Binary Search.

public class BinarySearch
{
 public static void main(String args[])
 {

 int[] ar = {87, 74, 40, 55, 31, 80, 34};
 int search = 40; // the element to be searched
 int start= 0;
 int mid;
 int last= ar.length - 1;
 while (start< = last)
 {
 mid= (start+ right) / 2;
 if (a[last] == search)
 {
 System.out.println("Element found at index " + mid);
 break;
 }
 else if (a[mid] < search)
 {
 start= mid+ 1;
 }
 else if (a[mid] > search)
 {
 last= mid- 1;
 }
 }
 }
}