Bitsmore360 - Top trends, Tech News, Tips and Tricks: Java Ques 101: How to print patterns in java?

Java Ques 101: How to print patterns in java?

In this article titled "Java Ques 101: How to print patterns in java?", we will learn to solve different types of pattern questions asked by the teachers and interviewers. Printing patterns are one of the most common questions asked by the teachers and the interviewers. Many of us fail to answer such questions, due to a lack of basic knowledge of pattern printing. For any programmer, pattern solving is one of the most interesting tasks in programming. Beginners can improve their coding skills, loop concepts, and different logic.


Java Ques 101: How to print patterns in java?

Java Ques 101: How to print patterns in java?


Also Read: How to remove blank rows from Excel

Also Read: How to split a cell diagonally in Excel


Java Ques 101: How to print patterns in java?

Pattern solving is of three types. Each pattern uses two loops. First Loop is used for printing number of column and the Second loop is used for printing number of rows. Following are the different types of patterns and their diagram. 

  • Star pattern- print a pattern using asterisk * sign.
  • Number pattern- print a number pattern from number ‘1-9’ .
  • Character pattern- print a character pattern from character ‘A-z’.

How to print * pattern in java?

fig: Different types of Pattern

Each pattern program consists of rows and columns and leading spaces. In simple pattern program, for handling rows and columns we need two variables. For Rows, we will use var i and for Columns, we will use var j. See the example given below.


Java Ques 101: How to print patterns in java?



In the given example, we have to print a simple right angle pattern using * asterisk sign. 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 star 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. 

            int rows = 5
//var i for rows
for(int i=0; i<row; i++)
{
//var j for columns
for(int j=0; j<=i; j++)
{
System.out.print("* ");
}
System.out.println();
}

 

In First Iteration, the value of i will be 0, it is less than 5 so condition becomes true and will execute inner statement. In inner statement, the value of j will be 0, it is less than or equal to the value of i, so condition becomes true and it will print star (*). After execution outer loop containing command System.out.println() will change the line. Both the loop will increment by one after the execution.

In Second iteration, the value of i will be 1, it is less than 5 so condition becomes true and will execute inner statement. In inner statement, the value of j will be 1, it is less than or equal to the value of i, so condition becomes true and it will print star (*). After execution outer loop containing command System.out.println() will change the line. Both the loop will increment by one after the execution.

In Third Iteration, the value of i will be 2, it is less than 5 so condition becomes true and will execute inner statement. In inner statement, the value of j will be 2, it is less than or equal to the value of i, so condition becomes true and it will print star (*). After execution outer loop containing command System.out.println() will change the line. Both the loop will increment by one after the execution.

In Fourth Ieration, the value of i will be 3, it is less than 5 so condition becomes true and will execute inner statement. In inner statement, the value of j will be 3, it is less than or equal to the value of i, so condition becomes true and it will print star (*). After execution outer loop containing command System.out.println() will change the line. Both the loop will increment by one after the execution.

In Fifth Iteration, the value of i will be 4, it is less than 5 so condition becomes true and will execute inner statement. In inner statement, the value of j will be 5, it is less than or equal to the value of i, so condition becomes true and it will print star (*). After execution outer loop containing command System.out.println() will change the line. Both the loop will increment by one after the execution.

Java Ques 101: Write a program in Java to print right angle triangle star pattern.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class Demo1{
public static void main(String args[]){
int row=5;
for(int i=0; i<row; i++)
{
for(int j=0; j<=i; j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}



Output:-

Java Ques 101: Write a program in Java to print right angle triangle star pattern.


Java Ques 101: Write a program in Java to print inverted right triangle star pattern.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class Demo2{
public static void main(String args[]) {
  
for(int i = 5; i > 0; i--) {
/* Prints one row of triangle */
for(int j = i; j > 0; j--) {
System.out.print("* ");
}
/* move to next row */
System.out.print("\n");
}
}
}





Output:-

Java Ques 101: Write a program in Java to print inverted right triangle star pattern.


Java Ques 101: Write a program in java to print pyramid pattern using star.


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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Demo3{
public static void main(String args[])
{
int sp,star=0;
int rows=6;
//outer loop for printing rows
for(int i = 1; i <= rows; i++)
{
// print leading space
for(sp = 1; sp <= rows-i; sp++) {
System.out.print(" ");
}// print star
while(star != (2*i - 1)){
System.out.print("*");
star++;
}
star=0;
// move to next row
System.out.print("\n");
}
}
}

Java Ques 101: How to print patterns in java?


Java Ques 101: Write a program in java to print inverted pyramid pattern using star.


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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class Demo4{
public static void main(String args[]){
int rows=6;
int space, star=0;
for(int i = rows;i >= 1; i--)
{// for leading space
for(space = 0; space <= rows-i; space++)
{
System.out.print(" ");
}
//print pattern
star = 0;
while(star != (2*i - 1))
{
System.out.print("*");
star++;
}
System.out.print("\n");
}
}
}

Java Ques 101: Write a program in java to print inverted pyramid pattern using star.





Java Ques 101: Write a program in C to print right triangle star pattern.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include<stdio.h>
int main(){
int i, j, n;
/* Input number of rows from user */
printf("Enter value of n: ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

 

Java Ques 101: Write a program in C++ to print right triangle star pattern.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
int main(){
int i, j, rows;
cout << "Enter number of Row = ";
cin >> rows;
cout << "Right Angle Triangle Star Pattern\n";
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++){
cout << "* ";
}
cout << "\n";
}
return 0;
}

 

Java Ques 101: Write a program in java to print the following pattern-


    1
    1 0
    1 0 1
    1 0 1 0
    1 0 1 0 1


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class Demo5
{
public static void main(String args[])
{
for(int i = 1; i <= 7; i++)
{
for(int j = 1; j <= i; j++)
{
if(j%2 == 0){
System.out.print(0);
    
}else{System.out.print(1);}


}
System.out.println();
}
}
}




Java Ques 101: Write a program in java to print the following pattern-


    1   2   3   4
    5   6   7   8
    9  10 11 12


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class Demo6
{
public static void main(String args[])
{
for(int i = 1; i <= 12; i++)
{
for(int j = 1; j <= 4; j++)
{
System.out.print("   "+i);
i++;
}
i--;
System.out.println();
}
}
}


Java Ques 101: Write a program in java to print the following pattern-


Java Ques 101: Write a program in java to print the following pattern-


101010
010101
101010
010101
101010


 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
27
28
29
public class Demo7
{
public static void main(String args[])
{
for(int i = 1; i <= 5; i++)
{
int n;
if(i%2 == 0)
{
n = 0;
for(int j = 1; j <= 5; j++)
{
System.out.print(n);
n = (n == 0)? 1 : 0;
}
}
else
{
n = 1;
for(int j = 1; j <= 6; j++)
{
System.out.print(n);
n = (n == 0)? 1 : 0;
}
}
 System.out.println();
}
}
}

 

Java Ques 101: Write a program in java to print the following pattern-


Java Ques 101: Write a program in java to print Pascal's Triangle





 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 DemoPascal{
public static void main(String args[])
{
System.out.println("Pascal's Triangle");
     
int n1=5;
int spaces= n1;
int number=1;

for(int i=0;i<n1;i++)
{
for(int s=1;s<=spaces; s++)
{
System.out.print(" ");
}
number=1;
for(int j=0;j<i;j++)
{
System.out.print(number + " ");
number=number*(i-j)/(j+1);
}
spaces--;
System.out.println();
}
}
}


Output:-

Java Ques 101 - Program to print Pascal's Triangle in Java




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.

 


Did you find this article helpful? OR Have any doubt, write in the comment section. To learn more about Bitsmore360, check out the rest of our Blog, and visit our Pinterest site and follow us on Instagram.





No comments:

Post a Comment