Showing posts with label Pattern. Show all posts
Showing posts with label Pattern. Show all posts

Alphabet pyramid pattern program in java



Alphabet pyramid pattern program in java


Alphabet pyramid pattern program in java


import java.util.Scanner;
public class AlphabetPyramid
{
 public static void main(String[] args) {
  char ch;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter pyramid character : ");
  ch = sc.next().charAt(0);
  if (ch >= 'a' && ch <= 'z') {
   ch = (char)(ch - 32);
  }
  for (char r = 'A'; r <= ch; r++) {
   for (int sp = (ch - r); sp >= 1; sp--) {
    System.out.print(" ");
   }
   for (char c = 'A'; c <= r; c++) {
    System.out.print(c);
   }
   for (char c = (char)(r - 1); c >= 'A'; c--) {
    System.out.print(c);
   }
   System.out.println();
  }
 }
}

Output:

Enter pyramid character : e

       A
      ABA
    ABCBA
  ABCDCBA
ABCDEDCBA


BUILD SUCCESSFUL (total time: 2 seconds)


Note: Hello friends, If you have any better solution for this programs then please comment below, I will add your comment as a solution for this programs.

Thanks for visiting my blog.


Square character triangle program in Java



import java.util.Scanner;
public class SquareCharacterTriangle
{
 public static void main(String[] args) {
  char ch;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter last character of triangle : ");
  ch = sc.next().charAt(0);
  if (ch >= 'a' && ch <= 'z') {
   ch = (char)(ch - 32);
  }
  System.out.println();
  for (char r = 'A'; r <= ch; r++) {
   for (int sp = ch - r; sp >= 1; sp--) {
    System.out.print(" ");
   }
   for (char c = 'A'; c <= r; c++) {
    System.out.print(c);
   }
   System.out.println();
  }
  for (char r = 'A'; 'A' <= ch - 1; ch--, r++) {
   for (int sp = r; sp >= 'A'; sp--) {
    System.out.print(" ");
   }
   for (char c = 'A'; c < ch; c++) {
    System.out.print(c);
   }
   System.out.println();
  }
 }
}

Output:

Enter last character of triangle : e

        A
      AB
    ABC
  ABCD
ABCDE
  ABCD
    ABC
      AB
        A


BUILD SUCCESSFUL (total time: 4 seconds)


Note: Hello friends, If you have any better solution for this programs then please comment below, I will add your comment as a solution for this programs.

Thanks for visiting my blog.



Character triangle pyramid program in Java



import java.util.Scanner;
public class CharacterTrianglePyramid {
 public static void main(String[] args) 
 {
  char ch;
  int sp;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter last character of triangle : ");
  ch = sc.next().charAt(0);
  if (ch >= 'a' && ch <= 'z') {
   ch = (char)(ch - 32);
  }
  System.out.println();
  for (char r = 'A'; r <= ch; ch--) {
   for (char c = ch; c >= r; c--) {
    System.out.print(c);
   }
   System.out.println();
  }
 }
}

Output:

Enter last character of triangle : e

EDCBA
DCBA
CBA
BA
A

BUILD SUCCESSFUL (total time: 5 seconds)


Note: Hello friends, If you have any better solution for this programs then please comment below, I will add your comment as a solution for this programs.

Thanks for visiting my blog.


Reverse star pyramid program in Java





package demo;
import java.util.Scanner;
public class ReverseStar
{
 public static void main(String[] args) 
 {
  int num, r = 1;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter loop repeat number(rows): ");
  num = sc.nextInt();
  System.out.println();
  for (; num >= 1; num--, r++) 
  {
   for (int c = 1; c <= num; c++) {
    System.out.print("*");
   }
   for (int sp = r; sp > 1; sp--) {
    System.out.print(" ");
   }
   for (int sp = r; sp > 1; sp--) {
    System.out.print(" ");
   }
   for (int c = num; c >= 1; c--) {
    System.out.print("*");
   }
   System.out.println();
  }
 }
}

Output:

Enter loop repeat number(rows): 7

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


BUILD SUCCESSFUL (total time: 3 seconds)




Nested Star Hash Pyramid Program in Java





package demo;
public class NestedStrarPyramid
{
 public static void main(String[] args) 
 {
  int n = 5;
  for (int r = 1; r <= 6; r++, n--) {
   /*first pyramid*/
   for (int c = 1; c <= n; c++) {
    System.out.print(" #");
   }
   /*second pyramid*/
   for (int c = 1; c <= r; c++) {
    if (c == 1) {
     System.out.print(" *");
    } else {
     System.out.print(" #");
    }
   }
   /*third pyramid*/
   for (int c = r; c > 1; c--) {
    if (c == 2) {
     System.out.print(" *");
    } else {
     System.out.print(" #");
    }
   }
   /*fourth pyramid*/
   for (int c = n; c >= 1; c--) {
    System.out.print(" #");
   }
   System.out.println();
  }
 }
}

Output:

run:

 # # # # # * # # # # #
 # # # # * # * # # # #
 # # # * # # # * # # #
 # # * # # # # # * # #
 # * # # # # # # # * #
 * # # # # # # # # # *


BUILD SUCCESSFUL (total time: 1 second)


E-Shape Pyramid Program in Java language





package demo;
public class EShape
{
 public static void main(String[] args) 
 {
  int num = 5;
  for (int r = 1; r <= num; r++) 
  {
   if (r % 2 == 0) {
    System.out.print("*");
   } else if (r % 3 == 0) {
    for (int c = 1; c <= r; c++) 
    {
     System.out.print("*");
    }
   } else {
    for (int c = 1; c <= num; c++) 
    {
     System.out.print("*");
    }
   }
   System.out.println();
  }
 }
}

Output:

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

BUILD SUCCESSFUL (total time: 1 second)


Character pattern pyramid program in Java





package demo;
import java.util.Scanner;
public class CharacterPattern
{
 public static void main(String[] args) 
 {
  char ch, r, c;
  System.out.println("Enter last pyramid character  : ");
  Scanner sc = new Scanner(System.in);
  ch = sc.next().charAt(0);
  if (ch >= 'a' && ch <= 'z') {
   ch = (char)(ch - 32);
  }
  for (r = 'A'; r <= ch; r++) {
   for (c = 'A'; c <= r; c++) {
    System.out.print(r + "" + (char)(r + 32) + " ");
   }
   System.out.println();
  }
 }
}

Output:

Enter last pyramid character  : 
e

Aa 
Bb Bb 
Cc Cc Cc 
Dd Dd Dd Dd 
Ee Ee Ee Ee Ee

BUILD SUCCESSFUL (total time: 7 seconds)


Java Program for Odd and Even Number Pattern.





package demo;
public class OddEvenPattern
{
 public static void main(String[] args) 
 {
  for (int r = 1, n = 1; r <= 5; r++, n = n + 2) 
  {
   if (r % 2 == 0) {
    for (int c = 1, p = 2; c <= n; c++, p = p + 2) 
    {
     System.out.print(" " + p);
    }
   } 
   else {
    for (int c = 1, p = 1; c <= n; c++, p = p + 2) 
    {
     System.out.print(" " + p);
    }
   }
   System.out.println();
  }
 }
}

Output:

1
 2 4 6
 1 3 5 7 9
 2 4 6 8 10 12 14
 1 3 5 7 9 11 13 15 17

BUILD SUCCESSFUL (total time: 0 seconds)