Computer Science, asked by salwynchristopher, 11 months ago

(i) M
MAT
MATH
MATHS
THIS IS THE QUESTION OF JAVA AND STRING HANDLING PATTERN

Answers

Answered by ridhimakh1219
1

Java program to print following pattern

M                                              

MA                   

MAT                                                      

MATH                    

MATHS

Explanation:

Java program to print pattern

import java.util.Scanner;

public class Main

{

   public static void main(String[] args)

   {

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter any String: "); \\ input string  

       String st = sc.nextLine();

       char [] arr = st.toCharArray(); // Store string st to array arr

       for (int i = 0; i < arr.length; i++)

       {

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

           {

               System.out.print(arr[j]); // print one character at a time

           }

           System.out.println();

       }

   }

}

OUTPUT

Enter any String: MATHS        

M                                              

MA  

MAT    

MATH                    

MATHS

Similar questions