Write a statement that calls the recursive method backwardsAlphabet() with param
ID: 3663201 • Letter: W
Question
Write a statement that calls the recursive method backwardsAlphabet() with parameter startingLetter.
public class RecursiveCalls {
public static void backwardsAlphabet(char currLetter) {
if (currLetter == 'a') {
System.out.println(currLetter);
}
else {
System.out.print(currLetter + " ");
backwardsAlphabet(--currLetter);
}
return;
}
public static void main (String [] args) {
char startingLetter = '-';
startingLetter = 'z';
BackwardsAlphabet(startingLetter);
return;
}
}
Explanation / Answer
As per given problem statement updated the below given source code to print the recursively from alphabet 'z' to 'a' by calling method backwardsAlphabet in main method..
1. Here Modifications are updated starting char from z to a ..
2. print the result after calling the method backwardsAlphabet method.
See the below code :
public class RecursiveCalls
{
public static void backwardsAlphabet(char ch)
{
if(ch=='z')
{
System.out.print("z ");
}
else
{
backwardsAlphabet((char) (ch+1));
System.out.print(ch+" ");
}
}
public static void main (String [] args)
{
char startingLetter = '-';
startingLetter = 'a';
new RecursiveCalls().backwardsAlphabet(startingLetter);
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.