Question

In: Computer Science

Write a recursive a Java code that checks if a number is Palindrome. A palindrome number...

Write a recursive a Java code that checks if a number is Palindrome. A
palindrome number is a number that reads the same from beginning to end and
from end to beginning, in other words, a palindrome number remains the same
when its digits are reversed. For example, 13431 is a palindrome number. 2332
is another one. (Note: Your algorithm should define and work with an integer
number)
The functionality of your code should be commented to explain what you do in
each part of the code.
You need to run your code for the following test cases and show the result:
1) 0          (output: yes)
2) 1234554321     (output: yes)
3) 123454321    (output: yes)
4) 1221       (output: yes)
5)   1234       (output: no)
6) 7676       (output: no)
7) -121      (output: yes)
8) -456      (output: no)
What is the time complexity of your algorithm? Cleary justify your
answer.

Solutions

Expert Solution

import java.util.*;
public class Main
{
   public static void main(String[] args) {
       Scanner s=new Scanner(System.in);
       int num=s.nextInt();//reading number
       int temp,res=0;
       if(num<0)//if it is negative make positive
       num=-num;
       temp=num;//assigning to temparory variable
       while(temp!=0)
       {
       res=(res*10)+temp%10;//making it reverse
       temp=temp/10;
       }
       if(res==num)//checking original and result number
       {
       System.out.println("output:yes");
       }
       else
       System.out.println("output:No");
   }
}

Time Complexity

The time complexity for this one is O(log n) because we are doing num%10 && num/10. so, these type of Operations gives log n complexity and remaining statements are considered constant 1 and finally it gives O(log n) complexity.


Related Solutions

Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
Please write a pep/9 assembly code that checks if a word or number is a palindrome
Please write a pep/9 assembly code that checks if a word or number is a palindrome
*Code in C* Write a function that checks if a number is a perfect cube. Write...
*Code in C* Write a function that checks if a number is a perfect cube. Write another function that calculates the integer cubic root. Under the main program: Prompt the user to input a number Tell the user if the number is a perfect cube or not Print the cubic root if the inputted number is a perfect cube.
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Create using Java Description: Palindrome -- According to wikipedia "A palindrome is a word, phrase, number...
Create using Java Description: Palindrome -- According to wikipedia "A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction" Write a application that can determine if a 5 digit number you input is a palindrome. If the number is a palindrome then print "The number is a palindrome." If it is not then print "The number is NOT a palindrome" Make sure to use an array Allow input...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT