forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeRecursion.java
More file actions
34 lines (32 loc) · 829 Bytes
/
PalindromeRecursion.java
File metadata and controls
34 lines (32 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Checking Palindrome number with Recursion
import java.util.Scanner;
class PalindromeRecursion
{
static long revNum=0; //stored reversed number
public static long palindrome(long i)
{ if(i>0) //base class
{ revNum=(revNum*10)+(i%10);
palindrome(i/10);
}
else
return revNum;
return revNum;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("N = ");
long n=sc.nextLong();
if(n==palindrome(n))
System.out.println(n+" is a Palindrome number");
else
System.out.println(n+" is not a Palindrome number");
}
}
/*
Sample Input and Output :
N = 153
153 is not a Palindrome number
Space Complexity: O(1)
Time Complexity : O(i) i=Number of digits in n
*/