mardi 12 février 2019

how to print the below pattern

for the given input I need to print the pattern .for example for input = 6. I have to print

MMMMMMSDDDDDD

MMMMMSSSDDDDD

MMMMSSSSSDDDD

MMMSSSSSSSDDD

MMSSSSSSSSSDD

MSSSSSSSSSSSD

CSSSSSSSSSSSK

CCSSSSSSSSSKK

CCCSSSSSSSKKK

CCCCSSSSSKKKK

CCCCCSSSKKKKK

CCCCCCSKKKKKK

I have tried but couldn't go further than this could anyone help

public class tgk {

public static void main(String[] args)  {

    Scanner sc = new Scanner(System.in);

    int size = sc.nextInt();

    int half = ((2*size)+1)/2;
     for (int i = 0; i < size ; i++) 
     { 
         for (int j = size; j > i; j--) 
         {
         System.out.print("M");
          }
     for (int k = half+1 ; k > half - i; k--) 
     {
         System.out.print("S");
     }






    System.out.println();
 }


     for(int i = size; i > 0; i--)
     {
         for (int j = size; j >= i; j--) {
             System.out.print("C");
         }
         for (int k = 0; k < (i * 2 - 1); k++) {
             System.out.print("S");
         }



         System.out.println();
     }
}

}

if input = 3 it should be

MMMSDDD

MMSSSDD

MSSSSSD

CSSSSSK

CCSSSKK

CCCSKKK

Aucun commentaire:

Enregistrer un commentaire