Computer Science, asked by khushboo5, 1 year ago

executed examples of nested for loops for patterns programming of java

Answers

Answered by Anonymous
2
The program is:-

class pattern
{
public static void main(String args[])
{
int s=5;
for (int i=1;i<=3;i++)
{
for(int j=2;j<=i;j++)
{
System.out.print("  ");
}
for(int k=1;k<=s;k++)
{
System.out.print("* ");
}
s=s-2;
System.out.println("");
}
s=3;
for (int i=1;i<=2;i++)
{
for(int j=1;j>=i;j--)
{
System.out.print("  ");
}
for(int k=1;k<=s;k++)
{
System.out.print("* ");
}
s=s+2;
System.out.println("");
}
}
}

Output:-

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

Hope it helps , if you want more programmings then ask in comments.

If it helps please mark it as brainliest.....








Answered by kvnmurty
0
Three patterns  STAR,  HOUR GLASS,  CIRCLE.
Patterns generated are enclosed as attachment.
=====================================

 

import java.util.*;

import java.lang.*;

import java.io.*;

class patterns_generate  {

public static void main (String[] args) {

        int i, j, N = 13, M =15, R=15;

       String s1=". ", s2="* ", s3="  ", s4="OO";

        // ---------- STAR

        for (j=0; j< N;j++) System.out.print(s1);  System.out.println();

        for (i=1; i < N-1;i++) {

            if(i==(N-1)/2){

                System.out.print(s1);

                for(j=1;j< N;j++)System.out.print(s2); System.out.println();

            }

            else {

                System.out.print(s1);

                for (j=1;j<N-1;j++) {

                     if ((i==j)||(i+j==N-1)||(j==N/2) ) System.out.print(s2);

                    else System.out.print(s3);

                }

                System.out.println(s1);

            }//else

        }   //for

        for (j=0;j<N;j++) System.out.print(s1);System.out.println();

       

        //  ------------ HOUR GLASS

        System.out.print("\n\n");

        for (i=0;i<M;i++){

            int k = i>M/2? M-i+1: i ;

            for(j=0;j<k;j++)System.out.print(s3);

            for (j=0;j<java.lang.Math.abs(M-2*i+1);j++) System.out.print(s4);

            for(j=M-i;j<M;j++)System.out.print(s3);

            System.out.println();

        }//for

        //-------------  CIRCLE

        System.out.print("\n\n");

        for (j=0;j<=2*R;j++){

            int k= (int)java.lang.Math.sqrt(2*R*j-j*j);

            for (i=0;i<=2*R+6;i++) {

                if (i==R+2-k || i == R+2+k || i==R && j==R+2) System.out.print("$$$");

                else System.out.print("   ");

            }

            System.out.println();

        }//for

    }//main

}//class

Attachments:
Similar questions