Check number is palindrome or not

Here given code implementation process.

//C Program
//Check number is palindrome or not
#include <stdio.h>

//Reverse The digits of number
int reverse(int number)
{
  int result = 0;

  while(number>0)
  {
    result=(result*10)+(number%10);
    //remove last digit
    number/=10;
  }
  return result;
}
//Check whether given number is palindrome or not
void is_palindrome(int number)
{
  //Compare number and its reverse form
  if(number == reverse(number))
  {
    //When both are equal
    printf("%d is palindrome\n",number );
  }
  else
  {
    //When not equal
    printf("%d is not palindrome\n",number );
  }
}
int main()
{
  //Test Case
  is_palindrome(121121);
  is_palindrome(1221);
  is_palindrome(1121); 
  is_palindrome(121);
  is_palindrome(9100109);
  is_palindrome(-101);
  return 0;
}

Output

121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is not palindrome
#include<iostream>

using namespace std;
/*
 C++ Program
 Check number is palindrome or not
*/
class MyNumber {
	public:

		//Reverse The digits of number
		int reverse(int number) {
			int result = 0;
			while (number > 0) {
				result = (result *10) + (number % 10);
				//remove last digit
				number /= 10;
			}
			return result;
		}
	//Check whether given number is palindrome or not
	void is_palindrome(int number) {
		//Compare number and its reverse form

		if (number == this->reverse(number)) {
			//When both are equal
			cout << number << " is palindrome\n";
		} else {
			//When not equal
			cout << number << " is not palindrome\n";
		}
	}
};
int main() {
	MyNumber obj;
	//Test Case
	obj.is_palindrome(121121);
	obj.is_palindrome(1221);
	obj.is_palindrome(1121);
	obj.is_palindrome(121);
	obj.is_palindrome(9100109);
	obj.is_palindrome(-101);
	return 0;
}

Output

121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is not palindrome
/*
  Java Program
  Check number is palindrome or not
*/
public class MyNumber {

  //Reverse The digits of number
  public int reverse(int number)
  {
    int result = 0;

    while(number > 0)
    {
      result=(result*10)+(number%10);
      //remove last digit
      number/=10;
    }
    return result;
  }
  //Check whether given number is palindrome or not
  public void is_palindrome(int number)
  {
    //Compare number and its reverse form
    if(number == reverse(number))
    {
      //When both are equal
      System.out.print(number+" is palindrome\n" );
    }
    else
    {
      //When not equal
      System.out.print(number+" is not palindrome\n" );
    }
  }

  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
    //Test Case
    obj.is_palindrome(121121);
    obj.is_palindrome(1221);
    obj.is_palindrome(1121); 
    obj.is_palindrome(121);
    obj.is_palindrome(9100109);
    obj.is_palindrome(-101);
  }
}

Output

121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is not palindrome
/*
  C# Program
  Check number is palindrome or not
*/
using System;
public class MyNumber {

	//Reverse The digits of number
	public int reverse(int number) {
		int result = 0;

		while (number > 0) {
			result = (result * 10) + (number % 10);
			//remove last digit
			number /= 10;
		}
		return result;
	}
	//Check whether given number is palindrome or not
	public void is_palindrome(int number) {
		//Compare number and its reverse form
		if (number == reverse(number)) {
			//When both are equal
			Console.Write(number + " is palindrome\n");
		} else {
			//When not equal
			Console.Write(number + " is not palindrome\n");
		}
	}

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		//Test Case
		obj.is_palindrome(121121);
		obj.is_palindrome(1221);
		obj.is_palindrome(1121);
		obj.is_palindrome(121);
		obj.is_palindrome(9100109);
		obj.is_palindrome(-101);
	}
}

Output

121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is not palindrome
# Python 3 Program
# Check number is palindrome or not
class MyNumber :
  # Reverse The digits of number
  def reverse(self, number) :
    result = 0
    while (number > 0) :
      result = (result * 10) + (number % 10)
      # remove last digit
      number = int(number /10)
    
    return result
  
  # Check whether given number is palindrome or not
  def is_palindrome(self, number) :
    # Compare number and its reverse form

    if (number == self.reverse(number)) :
      # When both are equal
      print(number ," is palindrome")
    else :
      # When not equal
      print(number ," is not palindrome")
    
  

def main() :
  obj = MyNumber()
  # Test Case
  obj.is_palindrome(121121)
  obj.is_palindrome(1221)
  obj.is_palindrome(1121)
  obj.is_palindrome(121)
  obj.is_palindrome(9100109)
  obj.is_palindrome(-101)


if __name__ == "__main__":
  main()

Output

121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is not palindrome
# Ruby Program 
# Check number is palindrome or not
class MyNumber 
	# Reverse The digits of number
	def reverse(number) 
		result = 0
		while (number > 0) 
			result = (result * 10) + (number % 10)
			# remove last digit
			number /= 10
		end
		return result
	end
	# Check whether given number is palindrome or not
	def is_palindrome(number) 
		# Compare number and its reverse form

		if (number == self.reverse(number)) 
			# When both are equal
			print(number ," is palindrome\n")
		else 
			# When not equal
			print(number ," is not palindrome\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	# Test Case
	obj.is_palindrome(121121)
	obj.is_palindrome(1221)
	obj.is_palindrome(1121)
	obj.is_palindrome(121)
	obj.is_palindrome(9100109)
	obj.is_palindrome(-101)
end
main()

Output

121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is not palindrome
/*
 Scala Program
 Check number is palindrome or not
*/
class MyNumber {
	//Reverse The digits of number
	def reverse(n: Int): Int = {
		var result: Int = 0;
      	var number: Int = n;
		while (number > 0) {
			result = (result * 10) + (number % 10);
			//remove last digit
          	number = (number /10).toInt;
		}
		return result;
	}
	//Check whether given number is palindrome or not
	def is_palindrome(number: Int): Unit = {
		//Compare number and its reverse form

		if (number == this.reverse(number)) {
			//When both are equal
			print(s"$number is palindrome\n");
		} else {
			//When not equal
			print(s"$number is not palindrome\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case
		obj.is_palindrome(121121);
        obj.is_palindrome(1221);
        obj.is_palindrome(1121);
        obj.is_palindrome(121);
        obj.is_palindrome(9100109);
        obj.is_palindrome(-101);
	}
}

Output

121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is not palindrome
/*
  Swift 4 Program
  Check number is palindrome or not
*/
class MyNumber {
	//Reverse The digits of number
	func reverse(_ n: Int) -> Int {
		var result: Int = 0;
     	var number: Int = n;
		while (number > 0) {
			result = (result * 10) + (number % 10);
			//remove last digit
			number /= 10;
		}
		return result;
	}
	//Check whether given number is palindrome or not
	func is_palindrome(_ number: Int) {
		//Compare number and its reverse form
		if (number == self.reverse(number)) {
			//When both are equal
			print(number ," is palindrome");
		} else {
			//When not equal
			print(number ," is not palindrome");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	//Test Case
	obj.is_palindrome(121121);
	obj.is_palindrome(1221);
	obj.is_palindrome(1121);
	obj.is_palindrome(121);
	obj.is_palindrome(9100109);
	obj.is_palindrome(-101);
}
main();

Output

121121  is palindrome
1221  is palindrome
1121  is not palindrome
121  is palindrome
9100109  is not palindrome
-101  is not palindrome
<?php
/*
  Php Program
  Check number is palindrome or not
*/
class MyNumber {
	//Reverse The digits of number

	public 	function reverse($number) {
		$result = 0;
		while ($number > 0) {
			$result = ($result *10) + ($number % 10);
			//remove last digit	
          	$number = intval($number / 10);
		}
		return $result;
	}
	//Check whether given number is palindrome or not

	public 	function is_palindrome($number) {
		//Compare number and its reverse form

		if ($number == $this->reverse($number)) {
			//When both are equal

			echo($number ." is palindrome\n");
		} else {
			//When not equal

			echo($number ." is not palindrome\n");
		}
	}
};

function main() {
	$obj = new MyNumber();
	//Test Case

	$obj->is_palindrome(121121);
	$obj->is_palindrome(1221);
	$obj->is_palindrome(1121);
	$obj->is_palindrome(121);
	$obj->is_palindrome(9100109);
	$obj->is_palindrome(-101);
}
main();

Output

121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is not palindrome
/*
 Node Js Program
 Check number is palindrome or not
*/
class MyNumber {
	//Reverse The digits of number
	reverse(number) {
		var result = 0;
		while (number > 0) {
			result = (result *10) + (number % 10);
			//remove last digit
          	number = parseInt(number / 10);
		}
		return result;
	}
	//Check whether given number is palindrome or not
	is_palindrome(number) {
		//Compare number and its reverse form

		if (number == this.reverse(number)) {
			//When both are equal
			process.stdout.write(number + " is palindrome\n");
		} else {
			//When not equal
			process.stdout.write(number + " is not palindrome\n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test Case
	obj.is_palindrome(121121);
	obj.is_palindrome(1221);
	obj.is_palindrome(1121);
	obj.is_palindrome(121);
	obj.is_palindrome(9100109);
	obj.is_palindrome(-101);
}
main();

Output

121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is not palindrome


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved