DEV Community

hema latha
hema latha

Posted on

Nested for loop pattern H and N

package Forloopbase;

public class Forloop {

public static void main(String[] args) {
    //pattern();
    //pattern1();
    //patternH();
    patternN();
    // TODO Auto-generated method stub
Enter fullscreen mode Exit fullscreen mode

}
private static void patternN() {
// TODO Auto-generated method stub
for(int line = 1; line<=9; line++)
{
for(int col=1; col<=9; col++)
{
if( col == 1 ||col==9 || line==col)
System.out.print("* ");
else

                System.out.print("  ");}
System.out.println();

}}

private static void patternH() {
    // TODO Auto-generated method stub
    for(int line = 1; line<=9; line++)
    {
        for(int col=1; col<=9; col++)
        {
if(  col == 1 ||col==9 ||line==5 )
            System.out.print("* ");
        else 

                System.out.print("  ");}
System.out.println();

}}
private static void pattern1() {
    // TODO Auto-generated method stub
    for(int line = 1; line<=9; line++)
    {
        for(int col=1; col<=9; col++)
        {
if( line==1 || line==9 || col == 1 ||col==9 ||line==col |line+col==10)
            System.out.print("* ");
        else 

                System.out.print("  ");}
System.out.println();
            }}

private static void pattern() {
    // TODO Auto-generated method stub
     for(int line = 1; line<=9; line++)
        {
            for(int col=1; col<=9; col++)
                {System.out.print(" * ");}
            System.out.println();
        }}}
Enter fullscreen mode Exit fullscreen mode

output=

Image description

out put

Image description

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs