1 2 1 3 2 1 4 3 2 1 3 2 1 2 1 1 java
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
1 2 1 3 2 1 4 3 2 1 3 2 1 2 1 1 java
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
pyramid pattern in java
// Java implementation to print the
// following pyramid pattern
public class Pyramid_Pattern {
// function to print the following pyramid
// pattern
static void printPattern(int n)
{
int j, k = 0;
// loop to decide the row number
for (int i = 1; i <= n; i++) {
// if row number is odd
if (i % 2 != 0) {
// print numbers with the '*'
// sign in increasing order
for (j = k + 1; j < k + i; j++)
System.out.print(j + "*");
System.out.println(j++);
// update value of 'k'
k = j;
}
// if row number is even
else {
// update value of 'k'
k = k + i - 1;
// print numbers with the '*' in
// decreasing order
for (j = k; j > k - i + 1; j--)
System.out.print(j + "*");
System.out.println(j);
}
}
}
// Driver program to test above
public static void main(String args[])
{
int n = 5;
printPattern(n);
}
}
1 2 1 3 2 1 4 3 2 1 3 2 1 2 1 1 java
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
pyramid pattern in java
// Java implementation to print the
// following pyramid pattern
public class Pyramid_Pattern {
// function to print the following pyramid
// pattern
static void printPattern(int n)
{
int j, k = 0;
// loop to decide the row number
for (int i = 1; i <= n; i++) {
// if row number is odd
if (i % 2 != 0) {
// print numbers with the '*'
// sign in increasing order
for (j = k + 1; j < k + i; j++)
System.out.print(j + "*");
System.out.println(j++);
// update value of 'k'
k = j;
}
// if row number is even
else {
// update value of 'k'
k = k + i - 1;
// print numbers with the '*' in
// decreasing order
for (j = k; j > k - i + 1; j--)
System.out.print(j + "*");
System.out.println(j);
}
}
}
// Driver program to test above
public static void main(String args[])
{
int n = 5;
printPattern(n);
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us