Import Public Class Public Static Void Null Double Int Try New in Out Out Out
Import Public Class Public Static Void Null Double Int Try New in Out Out Out
Scanner;
}
else if(option == 2) {
System.out.println("Enter Distance
in Miles");
distance =
Double.parseDouble(input.nextLine());
km = 1.60 * distance;
System.out.println("Distance in
Kilometers is:" + km );
}
}
catch(Exception e)
{
System.out.print("Error.Program
Terminated");
}
finally
{
if (input != null) {
input.close();
}
}
}
}
double kilometers;
}
}
double kilometers;
System.out.println("Please enter the kilometers");
kilometers = in.nextDouble();
double miles;
miles = in.nextDouble();
}
}
A string is said to be palindrome if it is the same from both the ends. For e.g. above
string is a palindrome because if we try to read it from backward, it is same as forward.
One of the approach to check this is iterate through the string till middle of string and
compare a character from back and forth.
ALGORITHM
o STEP 1: START
Program:
1. public class PalindromeString
2. {
3. public static void main(String[] args) {
4. String string = "Kayak";
5. boolean flag = true;
6.
7. //Converts the given string into lowercase
8. string = string.toLowerCase();
9.
10. //Iterate the string forward and backward, compare one character at a tim
e
11. //till middle of the string is reached
12. for(int i = 0; i < string.length()/2; i++){
13. if(string.charAt(i) != string.charAt(string.length()-i-1)){
14. flag = false;
15. break;
16. }
17. }
18. if(flag)
19. System.out.println("Given string is palindrome");
20. else
21. System.out.println("Given string is not a palindrome");
22. }
23. }
1. Original string: Dream big
2. Reverse of the string: big maerD
ALGORITHM
o STEP 1: START
o STEP 2: DEFINE String string = "Dream big"
o STEP 6: i = i - 1
o STEP 9: END
Program:
1. public class Reverse
2. {
3. public static void main(String[] args) {
4. String string = "Dream big";
5. //Stores the reverse of given string
6. String reversedStr = "";
7.
8. //Iterate through the string from last and add each character to variable re
versedStr
9. for(int i = string.length()-1; i >= 0; i--){
10. reversedStr = reversedStr + string.charAt(i);
11. }
12.
13. System.out.println("Original string: " + string);
14. //Displays the reverse of given string
15. System.out.println("Reverse of given string: " + reversedStr);
16. }
17. }
Output: