Computer Science, asked by adityasharma28, 11 months ago

java ke programs aate hai kisi ko

Attachments:

Answers

Answered by QGP
5

/* PATTERN QUESTIONS IN JAVA

* The pattern consists of 9 rows, with an increasing and decreasing size combination.

* We develop a general logic, apply it for an increasing sequence for first 5 rows

* Then we apply the same logic for the next decreasing 4 rows

*

* Here is a program which prints the pattern

* Explanations are given as comments

* Screenshots also attached

*

*/


public class P26_EpicPattern

{

   public static void main(String[] args)

   {

       

       for(int i=0;i<5;i++)

       {

           int ch=97;      //97 is the ASCII value for 'a'

           int nc=2*i+1;   //Number of characters which will go in a line

           boolean isReverse=false;    //Boolean value to see if we want to print letters in reverse

           

           for(int spc=0;spc<5-i;spc++)        //Loop for spaces. We need 5-i spaces

           {

               System.out.print(" ");  

           }

           

           if(i%2!=0)          //isReverse is set to True in alternate lines

           {

               isReverse=true;

           }

           

           if(isReverse)       //Condition of printing letters in reverse

           {  

               ch+=nc;         //nc added to ch to get the character to start the reverse series line

               for(int k=0;k<nc;k++)      

               {

                   System.out.print((char)(ch-1));     //(ch-1) is typecasted to char and printed

                   ch--;           //Decrementing the ASCII Value to print the next character in reverse series

               }

           }

           else            //Condition of printing letters in a normal series

           {

               for(int k=0;k<nc;k++)  

               {  

                   System.out.print((char)ch);     //ch simply typecasted to char and printed

                   ch++;           //Incrementing value of ch to print next character in series

               }

           }

           

           System.out.println();       //New Line statement

       }

       

       for(int i=3;i>=0;i--)       //Starting the decreasing triangle size loop. All inner contents are the same

       {

           int ch=97;

           int nc=2*i+1;

           boolean isReverse=false;

           

           for(int spc=0;spc<5-i;spc++)

           {

               System.out.print(" ");

           }

           

           if(i%2!=0)

           {

               isReverse=true;

           }

           

           if(isReverse)

           {

               ch+=nc;

               for(int k=0;k<nc;k++)

               {

                   System.out.print((char)(ch-1));

                   ch--;

               }

           }

           else

           {

               for(int k=0;k<nc;k++)

               {

                   System.out.print((char)ch);

                   ch++;

               }

           }

           

           System.out.println();

       }

   }

}

Attachments:

Anonymous: u r the most hardworking guy that I have ever seen!!
QGP: :)
Hakar: wow
Hakar: it's awesome
Similar questions