TheAlgorithms-C/misc/palindrome.c

41 lines
943 B
C
Raw Normal View History

2020-07-13 05:32:12 +03:00
/**
* @file
* @brief Program to identify if a number is [palindrome
* number](https://en.wikipedia.org/wiki/Palindrome) or not.
* @see project_euler/problem_4/sol1.c
*/
#include <assert.h>
#include <stdbool.h>
2016-12-13 12:18:53 +03:00
#include <stdio.h>
2020-07-13 05:32:12 +03:00
bool isPalindrome(int number);
2016-12-13 12:18:53 +03:00
2020-07-13 05:32:12 +03:00
/** Driver Code */
int main()
{
assert(isPalindrome(0));
assert(isPalindrome(1));
assert(isPalindrome(12321));
assert(!isPalindrome(1234));
return 0;
}
2016-12-13 12:18:53 +03:00
2020-07-13 05:32:12 +03:00
/**
* Check given number whether is palindrome number or not
* @param number number to check
* @return `true` if given number is palindrome number
* @return `false` if number is not a palindrome number
*/
bool isPalindrome(int number)
{
int reversedNumber = 0;
int originalNumber = number;
while (number != 0)
2016-12-13 12:18:53 +03:00
{
2020-07-13 05:32:12 +03:00
int remainder = number % 10;
reversedNumber = reversedNumber * 10 + remainder;
number /= 10;
2016-12-13 12:18:53 +03:00
}
2020-07-13 05:32:12 +03:00
return originalNumber == reversedNumber;
2016-12-13 12:18:53 +03:00
}