-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Palindrome_checker.js
67 lines (51 loc) · 1.22 KB
/
Palindrome_checker.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Write a program to check whether a given number is palindrome or not.
Palindrome is a number which reads the same backward as forward.
*/
//In-Built readline module
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const getLine = (function () {
const getLineGen = (async function* () {
for await (const line of rl) {
yield line;
}
})();
return async () => (await getLineGen.next()).value;
})();
function palindromechecker(no) {
let temp = no;
let reverse = 0;
//To reverse the number
while (temp !== 0) {
reverse = reverse * 10 + (temp % 10);
temp = parseInt(temp / 10);
}
if (no === reverse) console.log("Entered Number is a Palindrome");
else console.log("Entered Number is not a Palindrome");
}
const main = async () => {
//Taking Input of Number
console.log("Enter the Number");
var no = Number(await getLine());
//Calling palindrome checker function
palindromechecker(no);
//To close the program
process.exit(0);
};
main();
/*
Time Complexity: O(N)
Space Complexity: O(1)
Input 1:
1234321
Output 1:
Entered Number is a Palindrome
Input 2:
12345
Output 2:
Entered Number is not a Palindrome
*/