The following example shows how to Find a Palindrome Number in VB.NET. To begin with, let us first understand what a palindrome number is.
Basically, a palindrome number is a number that is equal to the number obtained by reversing the given number. Therefore, a palindrome number remains the same when we reverse its digits. Also, it is called a numeral palindrome. Further, the palindrome numbers have many applications including analyzing DNA, recreational mathematics, and so on.
How to Find a Palindrome Number
At first, we read a number from the user and make a copy of it so that we can compare the original number with the reversed one. Therefore, first we need to find the reverse of the given number.
In order to find the reverse, we use a While Loop that iterates till the number vanishes. Further, in each iteration, we extract the last digit of the number. Hence, we find the remainder of the number by dividing it by 10. After that, we compute a value by multiplying the remainder with some power of 10. As can be seen, the variable sum keeps the sum of all computed values in this manner.
Furthermore, in each step we calculate the value of 10 raise to the power of number of remaining digits. For this purpose, initially, we find the total number of digits by converting the number to a String and determining its length. Finally, in the last step, we divide the number by 10 so that, as soon as we retrieve all the digits, the number becomes zero.
In order to determine the palindrome, number we compare the sum with original number using an IF statement.
Module Module1 Sub Main() Dim n, n1, sum, rm, cnt As Integer Console.WriteLine("Enter a Number: ") n = CInt(Console.ReadLine()) n1 = n sum = 0 cnt = n1.ToString.Length - 1 While n1 <> 0 rm = n1 Mod 10 sum = sum + rm * CInt(Math.Pow(10, cnt)) cnt = cnt - 1 n1 = n1 10 End While If n = sum Then Console.WriteLine("Number is Palindrom") Else Console.WriteLine("Number is Palindrom") End If End Sub End Module