More Nested Loop Exercises Write methods that prints the fol

(More Nested Loop Exercises) Write methods that prints the following patterns to the console such that their size depend on a passed-in integer argument. This first shape is a triangular pattern with sides of 6 asterisks. The call that produces this pattern is shape7a(6);. Your code should work for any positive integer. This second shape is an \"X\" pattern with arms of 3 asterisks each. The call that produces this pattern is shape7b(3);. Your code should work for any positive integer.

Solution

public class Main{
   public static void main(String args[]){
       inverseTriangle(6);
       diagonal(3);
   }

   public static void inverseTriangle(int n){
       int i,j;
       for(i=0;i<=n;i++){
           for(j=0;j<n;j++){
               if(i>j){           // staring spaces
                   System.out.print(\" \");
               }
               else{
                   System.out.print(\"*\");
               }
           }
           System.out.println(\"\ \");
       }
   }
   public static void diagonal(int n){
       int i,j,k;
       int s=0,m=n*2 - 1;
       for(k=0;k<2;k++){
           for(i=0;i<n;i++){
               for(j=0;j<s;j++){
                   // starting spaces for a line
                   System.out.print(\" \");
               }
               System.out.print(\"*\");
               for(j=0;j<m;j++){
                   // middle spaces for a line
                   System.out.print(\" \");
               }
               System.out.print(\"*\");
               if(k==0){
                   // First half
                   s = s+1;
                   m = m-2;
               }
               else{
                   // second half
                   s = s-1;
                   m = m+2;
               }
               System.out.println();
           }
           if(k==0){
               for(i=0;i<n;i++){
                   System.out.print(\" \");
               }
               System.out.print(\"*\");
               System.out.println();
               s = n-1;
               m = 1;

           }
       }
   }
}

/* sample output

******

*****

****

***

**

*

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

*/

 (More Nested Loop Exercises) Write methods that prints the following patterns to the console such that their size depend on a passed-in integer argument. This
 (More Nested Loop Exercises) Write methods that prints the following patterns to the console such that their size depend on a passed-in integer argument. This

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site