Palindrome Number Checking Using TypeScript

I was facing problem to write basic programs like Palindrome number, Armstrong number, Prime number checking in JavaScript which I have already write many times in C language. I did little study and found that its easy to write using TypeScript which is the super set of JavaScript. I am assuming that you guys also already did palindrome number checking programming previously in other programming languages like C/C++, python, Java etc. In this article I will use TypeScript to do palindrome number checking using TypeScript. I guess you guys know the basics of Typescript. If no, that is also fine you can grab it easily. So, let’s start.

What is palindrome number?

If we reverse a number and the value of that number remains same then the number will be called Palindrome number.

Palindrome Number example: 12321, 121, 5463645, 111, 222, 32123 … etc

What is Typescript?

Basically, Typescript is a superset of JavaScript means Typescript content all properties of JavaScript. Typescript helps JavaScript developers to implement class, data types as we do in high level language. It makes easy to debug errors and simplify OOPs implementation in JavaScript.

It is very easy to learn if you know OOPs like class, interface etc. Its is lightweight and great for production. Now come to the article main point

To familiar more with typescript language can go and visit : Learn more

This program can be written on many ways. I have choose Typescript to implement the Palindrome number checking.

class Palindrome{
x: number=0;    // declare number variable
y: number=0;    // declare number variable
temp: number=0; // declare number variable
constructor(x: number=0){ 
 this.x = x;
 this.y = x;
}

checkPalindromic(){
do{
 this.temp = (this.x)%10 + (this.temp)*10;
 this.x = Math.floor(this.x/10);
}while(this.x>=1);

  if(this.y===this.temp){
    return "Number is Palindromic";
  }else{
    return "Number is not Palindromic";
  }
}
}



//create objects
let obj  = new Palindrome(56365);
let obj1 = new Palindrome(1234);
let obj2 = new Palindrome(12321);


console.log(obj.checkPalindromic());
console.log(obj1.checkPalindromic());
console.log(obj2.checkPalindromic());

Output:
--------------------------
Number is Palindromic
Number is not Palindromic
Number is Palindromic

Conclusion: Palindrome number checking is a basic program to practicing any programming language. Which develops logical thinking of a programmer and as well as enhance the language fluency. So, keep learning and keep practicing.

Leave a Reply

Your email address will not be published. Required fields are marked *