Algorithm and Program Palindrome Number in C/C++

Algorithm and Program Palindrome Number in C/C++

algorithm and program for palindrome number in C/C++

Palindrome means a word, phrase, or sequence that reads the same backwards as forwards. In simple words, the reverse of a word, phrase or sequence is same as original. For e.g. madam, 313, level, top spot, etc. In this post we will see algorithm and program for palindrome number in C/C++.

To check if a given number is palindrome or not we will have to calculate its reverse first. Then check with the original number. Let’s see the algorithm first and then we will se program.

1. Algorithm

1. START
2. PRINT "Enter a number :"
3. READ num
4. temp = num
5. REPEAT WHILE num > 0
        rem = num % 10
        rev = rev * 10 + rem       
        num = num / 10
   [END OF WHILE]
6. IF temp == rev THEN
        PRINT "ENTERED NUMBER IS PALINDROME"
   ELSE
        PRINT "ENTERED NUMBER IS NOT PALINDROME"
   [END OF IF]
7. STOP

2. Code

C program

//Program to check a number is a palindrome or not

#include<stdio.h>  

 int main()    
{    
    int num, rem, rev = 0, temp;    
    printf("\nEnter the number :");    
    scanf("%d",&num);    
    temp = num;   

    while(num>0)    
    {    
        rem = num%10;    
        rev = rev*10 + rem;    
        num = num/10;    
    } 
    
    if(temp == rev)    
        printf("\n%d is a palindrome",temp);    
    else    
        printf("\n%d is not a palindrome",temp);    

    return 0;  
} 

C++ program

//Program to check a number is a palindrome or not

#include <iostream>

using namespace std;

int main()
{
    int num, temp, rem, rev = 0;
    cout << "\nEnter a three-digit integer: ";
    cin >> num;
    temp = num;

     while(num>0)    
    {    
        rem = num%10;    
        rev = rev*10 + rem;    
        num = num/10;    
    } 

    if (rev == temp)
        cout << temp << " is a palindrome.";
    else
        cout << temp << " is not a palindrome.";

    return 0;
}

3. Output

Algorithm and Program Palindrome Number in C/C++
Algorithm and Program Palindrome Number in C/C++

Also See : Program for Reverse Number in C or C++.

4. Program Parts

  1. #include is a pre-processor directive. It is used to include header files.
  2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
  3. .h is an extension for header file.
  4. int is data type. It indicates that the program returns a value.
  5. main() is main function that indicates the compiler that the user written programs starts from here.
  6. num, rem, sum, temp are variable names of integer data type.
  7. while is looping structure in C/C++. It repeats the code inside it untill a specific condition is satisfied.
  8. print/cout is the keyword use to print a message.
  9. Scanf/cin is the keyword use to scan a value entered by user.
  10. return statement is use to return a value. If any error occurs the program will return 0

5. Explanation

The program will accept a number. Then it will store the accepted number in temp variable. Now the program will enter while loop to calculate reverse of entered number. In while loop it will divide the accepted number by 10 and the remainder will be stored in rem. rev is used to store the reverse number. Then the num will divide itself with 10 and store the quotient in num only. The loop will execute until the condition is satisfied. Once the condition is not satisfied it will come out of the loop. Now it will check if the original number (i.e. temp) is equal to the reverse (i.e. rev). If yes then it will print “The number is palindrome.” else it will print “The number is not a palindrome.”. Finally the program will stop execution.

Conclusion

So this was the algorithm and program for palindrome number in C/C++. There are many other possible ways to check palindrome of a number. If you know any of them do tell us in the comment section below. Hope you liked it. If yes then please do share it with your friends and programming buddies. Also tell us what other programs you would like us to post.

For more such content follow us on instagram. Visit again!

ErrorFreeProgram.

Leave a Reply

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