How can we construct a character pattern program using for loop in java?

Introduction:

Hii everyone, here we are going to construct a character pattern program using for loop in java.This program will be based on the diecasting of the integer into the character, for better understanding, we can say that programmers first priority is less wastage of memory spaces and for this reason, we are diecasting the character here.

Description Of Program:

It is very important to analyse the program first then move towards the construction of program since we are making our program user flexible so we will input a scanner here.The main logic to generate the character program is to diecast the integer into the character, we will take “A” in the loop and then concatenate it with the logic “k1-1”. Initially, we will take “i” loop for the rows and runs it up to “n” which is given by the user, “j” loop will be used for the ” ” i.e. spaces, then runs “k1” loop and then “k2” loop will run. In “k2” loop we will run the loop in the reverse order.Hope it would be helpful.

Thank You!

CODE :

package patternspackage;

import java.util.Scanner;
public class CharacterPattern {
public static void main(String[] args) {
System.out.println(“Enter the value of N”);
Scanner s=new Scanner(System.in);
int n=s.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
System.out.print(” “);
for(int k1=1;k1<=i;k1++)
{
char ch=(char)(‘A’+k1-1);
System.out.print(ch);
}
for(int k2=i-1;k2>=1;k2–)
{
char ch=(char)(‘A’+k2-1);
System.out.print(ch);
}
System.out.println();
}

}

}

OUTPUT :

Leave a Reply