Computer Applications
Write a Java program to accept a character and print all the characters following it in the reverse order(till a).
Sample input:
If the character entered is d.
Sample output:
d
c
b
a
Answer
import java.util.Scanner;
public class KboatReverse
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a letter: ");
char ch = in.next().charAt(0);
if (Character.isLetter(ch)) {
ch = Character.toLowerCase(ch);
for (char x = ch; x >= 'a'; x--) {
System.out.println(x);
}
}
else {
System.out.println("Invalid input");
}
}
}Output
Related Questions
Primitive data types are built in data types which are a part of the wrapper classes. These wrapper classes are encapsulated in the java.lang package. Non primitive datatypes like Scanner class are a part of the utility package for which an object needs to be created.
(a) To which package the Character and Boolean classes belong?
(b) Write the statement to access the Scanner class in the program.
Differentiate between boxing and unboxing.
A student is trying to convert the string present in x to a numerical value, so that he can find the square root of the converted value. However the code has an error. Name the error (syntax / logical / runtime). Correct the code so that it compiles and runs correctly.
String x= "25"; int y=Double.parseDouble(x); double r=Math.sqrt(y); System.out.println(r);