Home » Java programs

Java program to print pyramid of characters using class



Submitted by BalRam Dixit, on May 18, 2017
PYRAMID of characters printing in Java - This program will print the pyramid of characters till N lines, N will be taken as input from the user.

In this program, we are going to write java code to print the pattern; this is a pattern of characters printed in a pyramid type structure, here we have to read total number of lines of the pyramid through the user.

Consider the program

import java.util.Scanner;

public class Pattern2{
	private int num;
	public void setNum(int num){
		this.num=num;
	}
	public int getNum(){
		return this.num;
	}
	public void printAnswer(){
		for(int i=1;i<=getNum();i++){
			for(int j=1;j<=getNum()-i+1;j++){
				System.out.print("  ");
			}
			for(int j=1;j<=i;j++){
				System.out.print((char)(64+j)+" ");
			}
			for(int j=i-1;j>=1;j--){
				System.out.print((char)(64+j)+" ");
			}
			System.out.println();
		}
	}
	public void inputNum(){
        Scanner sc  =   new Scanner(System.in);
        System.out.print("Enter Number  : ");
        int num =   sc.nextInt();
		setNum(num);
	}
	public static void main(String[] ar){
		Pattern2 ob = new Pattern2();
		ob.inputNum();
		ob.printAnswer();
		
	}
}

Output

Enter Number : 5
          A 
        A B A 
      A B C B A 
    A B C D C B A 
  A B C D E D C B A 





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.