Posts

Showing posts with the label Patterns

Diamond inside Rectangle/square

Image
Write a java program to print following pattern: import java.util.Scanner; public class DiamondInsideSqure { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Row size :"); int n = sc.nextInt(); int states = n, space = 0; for (int i = 0; i < 2 * n-1; i++) { if(i==0) { states=states; space=space; } else if (i <n) { states = states - 1; space = space + 2; } else { states = states + 1; space = space - 2; } for (int j = 0; j < states; j++) { System.out.print("*"); } for (int k = 0; k < space; k++) { System.out.print(" "); } for (int j = 0; j < states; j++) { System.out.print("*"); } System.out.println(); } } }
Write a java program to print following pattern *********  *******   *****    ***     * import java.util.Scanner; public class StarPattern { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Row Size : "); int n=sc.nextInt(); for(int i=0;i<n;i++) { for(int j=0;j<i;j++) { System.out.print(" "); } for(int k=2*(n-i)-1;k>0;k--) { System.out.print("*"); } System.out.println(); } } }
Write a java program to print following pattern:-         A      ABA    ABCAB  ABCDABC import java.util.Scanner; public class CharPattern20 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Row size : "); int n=sc.nextInt(); for(int i=0;i<n;i++) { int k=1,l=2*i-1; for(int j=0;j<n-i;j++) { System.out.print(" "); } for(int j=0;j<l;j++) { System.out.print((char)(64+k++)+""); if(j==i-1) k=1; } System.out.println(); } } }
Write a java program to print following pattern          1        1 2 1      1 2 3 2 1    1 2 3 4 3 2 1  import java.util.Scanner; public class NumberPattern { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Row Size "); int n=sc.nextInt(); for(int i=0;i<n;i++) { int k=1,l=2*i-1; for(int j=0;j<n-i;j++) { System.out.print("  "); } for(int j=0;j<l;j++) { System.out.print(k+" "); if(j<l/2) k++; else k--; } System.out.println(); } } }
Write a java program to print following pattern:- 1 2 3 4 5  10 9 8 7 6  11 12 13 14 15  20 19 18 17 16  21 22 23 24 25  import java.util.Scanner; public class CountPattern27 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Size: "); int n=sc.nextInt(); int k=1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i%2 != 0) {   System.out.print(k+" "); } else { System.out.print((-j+i*n+1)+" "); } k++; } System.out.println(); } } }
Write a java program to print following pattern:- 1 3 2 4 5 6 10 9 8 7 11 12 13 14 15  import java.util.Scanner; public class CountPattern31 { static Scanner sc=new Scanner(System.in); public static void main(String[] args) { System.out.println("Enter Size : "); int n=sc.nextInt(); int k=1; for(int i=1;i<=n;i++) { int l=k+i-1; for(int j=1;j<=i;j++) { if (i%2 != 0) {   System.out.print(k+" "); } else { System.out.print(l+" "); } k++; l--; } System.out.println(); } } }
Write a java program to print following number pattern  1 2 3 4 5 11 12 13 14 15 21 22 23 24 25 16 17 18 19 20 6  7  8  9 10 import java.util.Scanner; public class NumberPattern { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Size: "); int n=sc.nextInt(); int k=0,l=1; for (int i = 1; i <= n; i++) { if(i <= n/2+1) { if(i!=1)    k=k+2; } else { if (k%2==0) { k--; } else { k=k-2; } } for (int j = 1; j <= n; j++) { System.out.print((k*n+j)+" "); } System.out.println(); } } }
Write a java program to print following pattern 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25  import java.util.Scanner; public class NumberPattern { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Size: "); int n=sc.nextInt(); for (int i = 0; i < n; i++) { int k=i+1; for (int j = 0; j < n; j++) { System.out.print(k+" "); k+=n; } System.out.println(); } } }
Image
Write  a java program to print following pattern and input given by user : import java.util.Scanner; public class StarPattern11 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Size: "); int n=sc.nextInt(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if(i==0 || j==0 || j==n-1 || i==n-1 || i==j || i==n-j-1 ) System.out.print("* "); else System.out.print("  "); } System.out.println(); } } }
Write a java program to print following pattern       *******         *****           ***            *            *          ***        *****      ******* import java.util.Scanner; public class StarPattern10 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter n value"); int n=sc.nextInt(); int sp=0, st=n; for(int i=1;i<=n;i++) { for(int j=1;j<=sp;j++) System.out.print(" "); for(int j=1;j<=st;j++) System.out.print("*"); if (i<=n/2){ sp++; st=st-2; } else { sp--; st=st+2; } System.out.println(); } } }

Patterns

Write a java program to print star pattern Output is following :  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  import java.util.Scanner;           public class StarPattern { static Scanner sc=new Scanner(System.in); public static void main(String[] args) { System.out.println("Enter n value"); int n=sc.nextInt(); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { System.out.print(" * "); } System.out.println(); } } }