In this article, "Java Ques 101 - Program to print Alphabet Pattern in Java", we will learn how to create a program that prints a Alphabet Pattern in Java. The pattern-based question is one of the most common questions asked by teachers and interviewers. To understand the program to print alphabet pattern or any other pattern, it is important to have some knowledge of patterns.
Java Ques 101 - Program to print Alphabet Pattern in Java
Logic to print Alphabet Pattern in Java
In Java, to print an Alphabet pattern as shown above we use a character variable called char (for "character"). Char variable stores the ASCII value of alphabets. For Uppercase letters, the ASCII Value starts from 65 to 90. Here 65 represent Uppercase A and 90 represent Z. For Lowercase letters, the ASCII Value starts from 97 to 122. Here 97 represent lowercase A and 122 represent z.
Each pattern program consists of rows and columns, with leading spaces in between. In simple pattern programs, we need two variables: one for rows and one for columns. The variables are named i and j in the diagram above..
In the given example, we have to print a simple right angle pattern using Alphabet. Earlier we have learnt to print pattern using Asterisk sign. In this program, we have taken two variables: var i and var j. Before solving any pattern program we will first see the number of rows and columns used in the program.
Here -
- No of rows = 5 rows
- No of column = 5 columns
Solution-
In order to print 5 rows we will initialize var i = 0, condition i<=5 and will increment the value of var i by one till the condition is true. Once the condition becomes false it will stop executing the statement. To print alphabet we will use another for loop. Initialize var j=0, condition j<=i and will increment the value of var j by one till the condition is true.
for(int i = 1; i <=5; i++){int ch = 65;for(int j = 1; j <= i; j++){System.out.print((char)ch);ch++;}System.out.println();}
Here is a simple program to print a to z:-
1 2 3 4 5 6 7 8 9 10 | public class Demopattern { public static void main(String args[]) { for(char ch = 'a'; ch <= 'z'; ch++) { System.out.println(ch); } } } |
Java Ques 101 - Program to print Alphabet Pattern in Java
Here is the source code of java program to print Alphabet Pattern in Java using For loop. In the given program two for loops are used. One for loop is used to print number of rows and other to print character from A to Z.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class Pattern1demo { public static void main(String args[]) { System.out.println("Alphabet pattern \n"); for(int i = 1; i <10; i++)//for printing 5 rows { //create local variable with ASCII value of A i.e 65 int ch = 65; for(int j = 1; j <= i; j++) { System.out.print((char)ch); //casting of int value to char for printing character ch++; } System.out.println(); } } } |
Output:-
Java Ques 101 - Program to print Alphabet Pattern in Java using While Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public class Pattern1demo { public static void main(String args[]) { System.out.println("Alphabet pattern \n"); int i = 1; int j = 1; while(i<10) { j=1; int ch = 65; while(j<=i) { System.out.print((char)ch); //casting of int value to char for printing character ch++; j++; } System.out.println(); i++; } } } |
Let us see some of the different Pattern questions asked by the teachers and the interviewer-
- Write a program in java to print a star pattern.
- Write a program in java to print a number pattern.
- Write a program in java to print a character pattern.
- How to print * pattern in java?
- How to print * pattern in python
- How to print * pattern in c
- How to print * pattern in java
- How to print * pattern in SQL?
- How to print * pattern in PHP?
- How to print patterns in Python?
- How to print patterns in Java?
- How to print patterns in C?
- How to print patterns in SQL?
- Write a program to print half pyramid of numbers
- Write a program to print inverted half pyramid of *
- Write a program to print full Pyramid of *
- Write a program to print Pascal's triangle
- Write a program to print Floyd's triangle.
No comments:
Post a Comment