APPLE
APE
A
Print this pattern in java
Answers
Answered by
6
Correct Pattern will be –
APPLE
APP
A
The code –
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));
}
}
- 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
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