Patterns are most important for interview point of view. To understand pattern problem you know only the basic tactic of how to solve pattern problem. In Java, it becomes a little bit easier to code patterns problems.
In this article we discuss 20 basic pattern problem in java.
Java Program: To Check If A Number Is Prime In Java
Lets get start :)
Patterns In Java
1. Left Triangle Star Pattern:
First, let us begin with the basic and the commonly asked pattern program in Java i.e This is the basic pattern in java to write its code . Their is only two loops are used one for row and another for column.
Let’s write the java code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
// write ur code here
int n = scn.nextInt();
for (int i = 1; i <= n; i++){
for(int j = 1; j <= i; j++){
System.out.print("*\t");
}
System.out.println();
}
}
}
2. Downward Triangle Star Pattern:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
// write ur code here
int n = scn.nextInt();
for (int i = n; i >= 1; i--){
for(int j = i; j >= 1; j--){
System.out.print("*\t");
}
System.out.println();
}
}
}
3. Right Triangle Star Pattern:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
int sp = n - 1;
int st = 1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= sp; j++){
System.out.print("\t");
}
for(int k = 1;k <= st; k++){
System.out.print("*\t");
}
System.out.println();
sp--;
st++;
}
}
}
4. Right down Mirror Star Pattern:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
int sp = 0;
int st = n;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= sp; j++){
System.out.print("\t");
}
for(int k = 1;k <= st; k++){
System.out.print("*\t");
}
System.out.println();
sp++;
st--;
}
}
}
5. Diamond Shape Pattern Program in Java:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
int sp = n / 2;
int st = 1;
for(int i = 1; i <= n; i++){
for(int j = 1;j <= sp; j++){
System.out.print("\t");
}
for(int k = 1;k <= st; k++){
System.out.print("*\t");
}
if(i <= n / 2){
sp--;
st += 2;
}
else{
sp++;
st -= 2;
}
System.out.println();
}
} }
6. Space Diamond:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
int tst = n + 1;
int st = tst / 2;
int sp = 1;
for(int i = 1; i <= n; i++){
// System.out.print(st + "," + sp);
for(int j = 1; j <= st; j++){
System.out.print("*\t");
}
for(int k = 1; k <= sp; k++){
System.out.print("\t");
}
for(int j = 1; j <= st; j++){
System.out.print("*\t");
}
if(i <= n / 2){
st--;
sp += 2;
}
else{
st++;
sp -= 2;
}
System.out.println();
}
} }
7. Left-Diagonal Star Pattern:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
int sp = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j < sp; j++){
System.out.print("\t");
}
System.out.print("*");
System.out.println();
sp++;
}
}
}
8. Right-Diagonal Star Pattern:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
int sp = n - 1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= sp; j++){
System.out.print("\t");
}
System.out.print("*");
System.out.println();
sp--;
}
}
}
9. Double-Diagonal Star Pattern:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
// write ur code here
int n = scn.nextInt();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(i+j == n + 1 || i == j){
System.out.print("*\t");
}
else{
System.out.print("\t");
}
}
System.out.println();
}
}
}
10. Diamond Star Pattern:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
// write ur code here
int n = scn.nextInt();
int os = n / 2;
int is = -1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= os; j++){
System.out.print("\t");
}
System.out.print("*\t");
for(int k = 1; k <= is; k++){
System.out.print("\t");
}
if(i > 1 && i < n){
System.out.print("*\t");
}
if(i <= n /2){
os--;
is += 2;
}
else{
os++;
is -= 2;
}
System.out.println();
}
}
}
11. Simple Number Program:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
int var = 1;
for(int i = 1; i <= n; i++){
for(int j = 1;j <= i; j++){
System.out.print(var + "\t");
var++;
}
System.out.println();
}
}
}
12. Fibonacci Number Pattern:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
int a = 0;
int b = 1;
int c;
for(int i = 1; i <= n; i++){
for(int j = 1;j <= i; j++){
System.out.print(a + "\t");
c = a + b;
a = b;
b = c;
}
System.out.println();
}
}
}
13. Simple Number Pattern:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
for(int i = 0; i < n; i++){
int icj = 1;
for(int j = 0;j <= i; j++){
System.out.print(icj + "\t");
int icj1 = icj * (i - j) / (j + 1);
icj = icj1;
}
System.out.println();
}
}
}
14. Multiplication Number Pattern:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
for(int i = 1; i <= 10; i++){
int v = n * i;
System.out.println(n + " * " + i + " = " + v);
}
}
}
15. Diamond Number Pattern:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int sp = n / 2;
int st = 1;
int val = 1;
// write ur code here
for(int i = 1; i <= n; i++){
for(int j = 1; j <= sp; j++){
System.out.print("\t");
}
int cval = val;
for(int k = 1; k <= st; k++){
System.out.print(cval + "\t");
if (k <= st / 2){
cval++;
}
else{
cval--;
}
}
if(i <= n /2){
sp--;
st += 2;
val++;
}
else{
sp++;
st -= 2;
val--;
}
System.out.println();
}
}
}
16. Half-Space Diamond Number Pattern:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
int st = 1;
int sp = (2 * n) - 3;
for(int i = 1; i <= n; i++){
int val = 1;
for(int j = 1; j <= st; j++){
System.out.print(val + "\t");
val++;
}
for(int k = 1; k <= sp; k++){
System.out.print("\t");
}
if(i == n){
st--;
val--;
}
for(int l = 1; l <= st; l++){
val--;
System.out.print(val + "\t");
}
sp -= 2;
st++;
System.out.println();
}
}
}
17. Special Cut Diamond Star Pattern:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
// write ur code here
int n = scn.nextInt();
int st = 1;
int sp = n / 2;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= sp; j++){
if(i == n / 2 + 1){
System.out.print("*\t");
}
else{
System.out.print("\t");
}
}
for(int j = 1; j <= st; j++){
System.out.print("*\t");
}
if(i <= n/2){
st++;
}
else{
st--;
}
System.out.println();
}
}
}
18. Sandglass Star Pattern:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
int st = n;
int sp = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= sp; j++){
System.out.print("\t");
}
for(int j = 1; j <= st; j++){
if(i > 1 && i <= n / 2 && j > 1 && j < st){
System.out.print("\t");
}
else{
System.out.print("*\t");
}
}
for(int j = 1; j <= sp; j++){
System.out.print("\t");
}
if(i <= n/2){
sp++;
st -= 2;
}
else{
sp--;
st +=2;
}
System.out.println();
}
}
}
19. Swastik Star Pattern:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// write ur code here
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(i == 1){
if(j <= n /2 + 1 || j == n){
System.out.print("*\t"); }
else{
System.out.print("\t"); }
}
else if(i <= n / 2){
if(j == n /2 + 1 || j == n){
System.out.print("*\t");
}
else{
System.out.print("\t");
}
}
else if(i == n / 2 + 1){
System.out.print("*\t");
}
else if(i < n){
if(j == n /2 + 1 || j == 1){
System.out.print("*\t");
}
else{
System.out.print("\t");
}
}
else{
if(j >= n /2 + 1 || j == 1){
System.out.print("*\t");
}
else{
System.out.print("\t");
}
}
}
System.out.println();
}
}
}
20. W Star Pattern:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
// write ur code here
int n = scn.nextInt();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(j == 1 || j == n){
System.out.print("*\t");
}
else if(i > n / 2 && (i == j || i + j == n + 1)){
System.out.print("*\t");
}
else{
System.out.print("\t");
}
}
System.out.println();
}
}
}
Comments
Post a Comment
Thanks for the comment.