Computer Science, asked by abhijitrishabh, 5 hours ago

APPLE
APE
A
Print this pattern in java​

Answers

Answered by anindyaadhikari13
6

\texttt{\textsf{\large{\underline{Solution}:}}}

Correct Pattern will be –

APPLE

APP

A

The co‎‎de –

\rule{300}{2}

public class APPLE{

   public static void main(){

       String str="APPLE";

       for(int i=str.length();i>0;i-=2)

           System.out.println(str.substring(0,i));

   }

}

\rule{300}{2}

\texttt{\textsf{\large{\underline{Explanation}:}}}

  • str stores the string.
  • substring (x , y) method returns a new string that starts at index x and ends at index y.
  • Here, we are displaying substring of the string from first index to i'th index where i = {5,3,1}

See the attachment for output.

Attachments:
Answered by kamalrajatjoshi94
0

Answer:

Program:-

/*APPLE

APP

A/*

import java.util.*;

public class Main

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

int p;

String st="APPLE";

p=st.length();

char ch;

System.out.println("The pattern:");

for(int i=p;i>=0;i=i-2)

{

System.out.println(st.substring(0,i));

}

}

}

Attachments:
Similar questions