Pattern Program
Ques: This pattern you want to print on console.
*
* *
* * *
* * * *
Sol: Steps you need to follow to build any pattern in java
1. first draw vertically and horizontally lines on the pattern.it seems to be triangle but we have to consider like it’s a rectangular.
So picture will be clear more then we assume i =rows and j = columns.
2. position where we want to print * so it will be judge by i and j values
Program:
public class TrianglePattern {
public static void printTrianglePattern() {
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
if (j <= i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args) {
TrianglePattern.printTrianglePattern();
}
}
Ques: This pattern you want to print on console.
*****
****
***
**
*
Sol:
Program:
public class TrianglePattern {
public static void printTrianglePattern() {
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
if (j <= 6-i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args) {
TrianglePattern.printTrianglePattern();
}
}
Output
*****
****
***
**
*
Ques: This pattern you want to print on console.
*
**
***
****
*****
Sol:
Program:
public class TrianglePattern {
public static void printTrianglePattern() {
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
if (j >= 6-i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args) {
TrianglePattern.printTrianglePattern();
}
}
Output
*
**
***
****
*****
Ques: This pattern you want to print on console.
*
***
*****
*******
*********
Sol: Steps you need to follow to build any pattern in java
Program:
public class TrianglePattern {
public static void printTrianglePattern() {
for (int i = 1; i < =5; i++) {
for (int j = 1; j < =5; j++) {
if (j >= 6-i && j <=4+i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args) {
TrianglePattern.printTrianglePattern();
}
}
Output
*
***
*****
*******
*********
Ques: This pattern you want to print on console.
Sol: Steps you need to follow to build any pattern in java
Program:
public class TrianglePattern {
public static void printTrianglePattern() {
for (int i = 1; i < =5; i++) {
for (int j = 1; j < =5; j++) {
if (j <= 6-i && j >=4+i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args) {
TrianglePattern.printTrianglePattern();
}
}
Output
*********
**** ****
*** ***
** **
* *
Ques:
*
* *
* * *
* * * *
* * * * *
Sol:
Program:
public class TrianglePattern {
public static void printTrianglePattern() {
for (int i = 1; i < =5; i++) {
boolean k = true;
for (int j = 1; j < =5; j++) {
if (j >= 6-i && j <=4+i && k) {
System.out.print("*");
k = false;
}
else {
System.out.print(" ");
k = true;
}
}
System.out.println();
}
}
public static void main(String[] args) {
TrianglePattern.printTrianglePattern();
}
}
Output
*
* *
* * *
* * * *
* * * * *
Ques:
Sol:
Program:
public class XPattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (i == j || (i+j==6 && i>=3))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
Ques:
Sol:
Program:
public class HalfXPattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if ((i == j && i<=3)|| (j+i)==6)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
Ques:
Sol:
Program:
public class HeartPattern {
public static void main(String[] args) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
if ((i == 0 && (j%3!=0)) || (i == 1 && (j%3==0)) || (i-j==2) || (i+j==8))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
Ques:
Sol:
Program:
public class HollowDiamondShape {
public static void main(String[] args) {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if ((i+j==3) || (i+j==9) || (j-i==3) || (i-j==3))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
Ques:
Sol:
Program:
public class AXPattern {
public static void main(String[] args) {
int count;
for (int i = 0; i < 3; i++) {
count = 0;
for(int j = 0; j < 3-i-1; j++) {
System.out.print(" ");
}
for(int j = 0; j < i+1; j++) {
System.out.print("*");
if(count< i) {
System.out.print("A");
count++;
}
}
System.out.println();
}
}
}
Ques:
Sol:
Program:
public class FloydTriangle {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
count++;
System.out.print(count);
}
System.out.println();
}
}
}
Ques:
Sol:
Program:
public class Pattern4 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
for (int k = i-1; k >= 1; k--) {
System.out.print(k);
}
System.out.println();
}
}
}
Ques:
Sol:
Program:
public class Pattern6 {
public static void main(String[] args) {
int number ;
for (int i = 1; i <= 5; i++) {
number = i;
for (int j = 1; j <= i; j++) {
System.out.print(number);
number = number+5-j;
}
System.out.println();
}
}
}
Ques:
Sol:
Program:
public class PascalTriangle {
public static int pascalNum(int i, int j) {
if (i == j || j == 1)
return 1;
else
return pascalNum(i - 1, j - 1) + pascalNum(i - 1, j);
}
public static void printPascal() {
for (int i = 1; i <= 5; i++) {
for (int space = 1; space <= 5 - i; space++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(pascalNum(i, j));
}
System.out.println();
}
}
public static void main(String[] args) {
printPascal();
}
}
Ques:
Sol:
Program:
public class HollowTrinaglePattern {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 7; j++) {
if (i == 4 || (i+j==5) || (j-i==3))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}