Check Palindrome Number in C program

program-to-check-palindrome-number-in-c-language

Program Description:

We have looked at the LCM of two numbers program in our earlier articles, In today’s article, We are going to write a program to check Palindrome Number in C programming language. The program needs to accept a number from the user and check it is a palindrome number.

What is a Palindrome Number?

The Palindrome Number is a number that is equal to the original number when its digits are Reversed.

Here is an example.

Number 1234321, The Number 1234321 is a Palindrome number, As if you reverse the number, It will be the same as the Original number.

Similarly, The Number 89098 is a Palindrome number.

Now, We know what are the palindrome numbers, So let’s get started with the program.

Example Inputs and Outputs of the Program:

Example 1: Input:

Enter a positve numbers: 76967

Output:

76967 is a Palindrome Number

Example 2: Input:

Enter a positve numbers: 237384

Output:

237384 is not a Palindrome Number

Program Pre-Requisites:

We are going to use the C Loops and the Arithmetic Operators. So it is recommended to have the knowledge of them. Please go through the following blogs to learn more about the above topics.

Palindrome number in C Program Algorithm:

The main Idea is to Reverse the Original number and Then check the original number and reversed number. If both are Equal, Then the number is Palindrome. If both are not equal, Then the number is not a Palindrome number.

Here is the step-by-step algorithm:

  1. Start the program by taking the user input. Take the number from the user and store it in a variable named num
  2. We only accept positive numbers, So check if the number is positive, If not display the error message – Invalid Input. Please provide valid number
  3. Take a backup of the num for later comparison. temp = num
  4. Now use the Reverse Number in C Program logic to reverse the number using the Modulus and division operators
    • We use the Loop to iterate over each digit from the last to first
    • Perform the Modulus operation on the number to get the last digit – rem = temp % 10;
    • Then update the reverse number using rev_num = (rev_num * 10) + rem ;
    • Finally, remove the last digit of the number using the division operator using temp = temp / 10;
    • This above step-4 continues until the temp is greater than zero. temp > 0.
  5. Once step 4 is completed, We will get the reversed number in the variables rev_num.
  6. Compare the reversed number rev_num with the original number num using the num == rev_num
    • If the above comparison is True, Then both numbers i.e Original number and Reversed numbers are equal, and the Given number is a Palindrome Number. So display the result on the console.
    • If the above comparison is False, Then the number num is not a Palindrome number.
  7. STOP.

Palindrome number in C Program using while loop:

Here is the Check or Detect Palindrome number program using the while loop.

Program Output:

Compile and run the program using your IDE. we are using the GCC compiler under the Ubuntu Operating system.

$ gcc palindrome_number.c

Test the program.

As expected, Program is giving the desired results.

palindrome-number-in-c-program-output

Let’s try to input the negative numbers to the program

As we can see from the above output, The program is displaying the error message on the Negative number.

Palindrome number in C using for loop:

Let’s rewrite the above program using the for loop instead of the while loop. The program algorithm is going to be the same but the program syntax is going to change.

Here is the C code with for loop.

Program Output:

Compile and run the program.

Here is the output of the program for positive numbers

c-program-to-check-palindrome-number

Program output for the negative numbers.

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] C Program to Check Palindrome Number […]

  2. […] C Program to Check Palindrome Number […]

  3. […] C Program to Check Palindrome Number […]

Leave a Reply