Showing posts with label Pyramid. Show all posts
Showing posts with label Pyramid. Show all posts

Star pyramid program in java



Star pyramid program in java


Star pyramid program in java


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

Output:

Enter number of row : 5

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


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.



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.


Character triangle program in Java



Character triangle program in Java


Character triangle program in Java


import java.util.Scanner;
public class CharacterTriangle
{
 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 = r; sp > 'A'; sp--) {
    System.out.print(" ");
   }
   for (char c = r; c <= ch; c++) {
    System.out.print(c);
   }
   for (char c = (char)(ch - 1); r <= c; c--) {
    System.out.print(c);
   }
   System.out.println();
  }
 }
}

Output:

Enter pyramid character : e

ABCDEDCBA
  BCDEDCB
    CDEDC
      DED
        E


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.


Number pyramid program in Java



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

Output:

Enter number of rows : 6

            1
         1  2
       1  2  3
     1  2  3  4
   1  2  3  4  5
 1  2  3  4  5  6


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.


Number rhombus pattern program in Java



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

Output:

Enter number of rows : 5
        1
      121
    12321
  1234321
123454321
  1234321
    12321
      121
        1
     

BUILD SUCCESSFUL (total time: 2 seconds)



Character triangle pyramid program in Java



import java.util.Scanner;
public class CharacterTriangle
{
 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);
  }
  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 last character of triangle : 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.


Reverse character triangle pyramid program in Java



import java.util.Scanner;
public class CharacterTrianglePyramid
{
 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'; 'A' <= ch; ch--, r++) {
   for (int sp = r; sp > 'A'; sp--) {
    System.out.print(" ");
   }
   for (char c = 'A'; c <= ch; c++) {
    System.out.print(c);
   }
   for (char c = (char)(ch - 1); c >= 'A'; c--) {
    System.out.print(c);
   }
   System.out.println();
  }
 }
}

Output:

Enter last character of triangle : e

ABCDEDCBA
  ABCDCBA
    ABCBA
      ABA
        A

BUILD SUCCESSFUL (total time: 7 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.



Number triangle pyramid program in Java





import java.util.Scanner;
public class NumberTriangle
{
 public static void main(String[] args) 
 {
  int num;
  System.out.print("Enter loop repeat number(rows): ");
  Scanner sc = new Scanner(System.in);
  num = sc.nextInt();
  for (int r = 1; r <= num; r++) {
   for (int sp = 1; sp <= num - r; sp++) {
    System.out.print("  ");                         //it is 2 blank space
   }
   for (int c = 1; c <= 2 * r - 1; c++) {
    System.out.print(" " + c);
   }
   System.out.println();
  }
 }
}

Output:

Enter loop repeat number(rows): 5

         1
       1 2 3
     1 2 3 4 5
   1 2 3 4 5 6 7
 1 2 3 4 5 6 7 8 9


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.


Number triangle pyramid program in Java.





import java.util.Scanner;
public class NumberTraingle
{
 public static void main(String[] args) 
 {
  int num;
  System.out.print("Enter number of rows : ");
  Scanner sc = new Scanner(System.in);
  num = sc.nextInt();
  for (int r = 1; r <= num; r++) {
   for (int sp = num - r; sp > 0; sp--) {
    System.out.print(" ");
   }
   for (int c = 1; c <= r; c++) {
    System.out.print(r);
   }
   for (int k = 2; k <= r; k++) {
    System.out.print(r);
   }
   System.out.println();
  }
 }
}

Output:

Enter loop repeat number(rows): 5

         1
       1 2 3
     1 2 3 4 5
   1 2 3 4 5 6 7
 1 2 3 4 5 6 7 8 9

BUILD SUCCESSFUL (total time: 3 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.


Empty triangle pyramid program in Java.





package demo;
import java.util.Scanner;
public class BlankPyramid
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int num;
  System.out.print("Enter no of rows: ");
  num = sc.nextInt();
  System.out.println("*");
  for (int r = 1; r <= num; r++) 
  {
   System.out.print("*");
   for (int sp = 1; sp < r; sp++) 
   {
    System.out.print(" ");
   }
   System.out.println("*");
  }
  for (int j = 1; j <= num + 2; j++) 
  {
   System.out.print("*");
  }
 }
}

Output:
Enter no of rows: 4

*
**
* *
*  *
*   *

******

BUILD SUCCESSFUL (total time: 3 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.


Star triangle frame pyramid program in Java.





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

Output:

Enter number of rows: 4

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

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 Number triangle pyramid program in Java.





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

Output:

Enter any number : 6

1 2 3 4 5 6 
 2 3 4 5 6 
  3 4 5 6 
   4 5 6 
    5 6 
     6 

BUILD SUCCESSFUL (total time: 4 seconds)


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)




Java program to print alternative number-star pyramid.





package demo;
import java.util.Scanner;
public class NumberWithStar
{
 public static void main(String[] args) 
 {
  int num, z = 1, p, n;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter maximum number : ");
  //Input 5 for desire output
  num = sc.nextInt();
  for (int r = 1; r <= num; r++, z = z + 2) 
  {
   for (int c = 1; c <= z; c++) 
   {
    if (c % 2 == 0) {
     System.out.print("*");
    } else {
     System.out.print(r);
    }
   }
   System.out.println();
  }
  n = num + 3;
  p = num - 1;
  for (int r = num; r >= 1; r--, n = n - 2, p--) 
  {
   for (int c = 1; c <= n; c++) 
   {
    if (c % 2 == 0) {
     System.out.print("*");
    } else {
     System.out.print(p);
    }
   }
   System.out.println();
  }
 }
}

Output:

Enter maximum number : 5

1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4*
3*3*3*
2*2*
1*


BUILD SUCCESSFUL (total time: 5 seconds)