Learn Programming through Logical Thinking Series – 9

Learn Programming through Logical Thinking Series – 9

This blog explains about Learn Programming through Logical Thinking Series – 9 and is given below :

_______________________________________________________________________________

In this series, probably this could be the toughest program I ever tried.  Already our colleague Srilatha Akka shared the logic for this program in the previous post  in C Language to solve this problem.  Well, First of all – What is the problem to be solved?  Let me share you that!

ABCD Pattern

The above pattern should be printed.  I am sharing my logic here with for deriving the above pattern here with.

ABCD Pattern Program in Java

public class AsciiTry {
public static void main(String[] args) {
int i=0,j,k,r,s,l,m;
while(i<5){
j=0;
while(j<(i*1)){
System.out.print(” “);
j++;
}
k=0;

while(k<=(i*0)){
r=65+i;
System.out.printf(“%c”,r);
k++;

}
l=0;
while(l<7-(i*2)){
System.out.print(” “);
l++;
}
m=0;
while(m<=(i*0)){
s=73-i;
if(s==69)
break;
System.out.printf(“%c”,s);
m++;
}
l=7;
while(l>7-((i*2)-1)){
System.out.print(” “);
l–;
}
k=0;
while(k<=(i*0)){
r=73+i;
if(r==73){
break;
}
System.out.printf(“%c”,r);
k++;
}
l=0;
while(l<7-(i*2)){
System.out.print(” “);
l++;

}
m=0;
while(m<=(i*0)){
s=81-i;
if(s==77)
break;
System.out.printf(“%c”,s);
m++;
}
l=7;
while(l>7-((i*2)-1)){
System.out.print(” “);
l–;
}
k=0;

while(k<=(i*0)){
r=81+i;
if(r==81)
break;
System.out.printf(“%c”,r);
k++;
}
l=0;
while(l<7-(i*2)){
System.out.print(” “);
l++;
}
m=0;
while(m<=(i*0)){
s=89-i;
if(s==85)
break;
System.out.printf(“%c”,s);
m++;
}
l=7;
while(l>7-((i*2)-1)){
System.out.print(” “);
l–;
}
k=0;

while(k<=(i*0)){
r=89+i;

if(r==89)
break;
if(r==91)
break;
if(r==92)
break;
if(r==93)
break;
System.out.printf(“%c”,r);
k++;

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

_______________________________________________________________________________

Program to print patter like ‘W’ in java

import java.util.Scanner;

public class Pattern23
{
	// create class for printing "*" star.
	private static void stars(int count)
	{
		for (int i = 0; i < count; ++i)
	    System.out.print("*");
	}
	
	// create class for printing " " space.
	private static void spaces(int count)
	{
	    for (int i = 0; i < count; ++i)
	    System.out.print(" ");
	}
	 
	public static void main(String[] args)
	{
		// initialize and create object.
		int n;
		Scanner s=new Scanner(System.in);
	      
	    // enter number of rows.
	    System.out.print("Enter the number for pattern : ");
	    n=s.nextInt();
	    
	    for (int i = 0; i < n; ++i) 
	    {
	        stars(i + 1);
	        spaces(n - i - 1);
	        stars(n - i + 1);
	        spaces(2 * i);
	        stars(n - i);
	        spaces(n - i - 1);
	        stars(i + 1);
	 
	        System.out.println();
	    }
	}
}

Output

Enter the number for pattern : 5

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

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

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

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

*******            ******
_____________________________________________________

REFERENCES :


https://www.includehelp.com/java-programs/print-w-pattern-using-stars.aspx