× Home
Next Lec → ← Previous Lec

Check Palindrome Exercise

palindrome is a string or number which is same when we read it from back to front
So you have take input a number from user and check if it is a palindrome or not
Your task is to implement a function which returns 1 if a number is a palindrome and return 0 if not

                        

                                #include <stdio.h>
                                #include <string.h>
                                
                                int isPalindrome(char num[])
                                {
                                    int len = strlen(num);
                                    int res;
                                    for (int i = 0; i < (len / 2); i++)
                                    {
                                        if (num[i] == num[len - 1])
                                        {
                                            res = 1;
                                            len--;
                                        }
                                        else
                                        {
                                            res = 0;
                                        }
                                    }
                                    return res;
                                }
                                
                                int main()
                                {
                                    char num[10] = "";
                                    printf("Enter a number to check whether it is a palindrome or not :");
                                    scanf("%s", &num);
                                
                                    if (isPalindrome(num))
                                    {
                                        printf("This number is a palindrome\n");
                                    }
                                    else
                                    {
                                        printf("This number is not a palindrome");
                                    }
                                    return 0;
                                }
                        
                    
                        

                               #include <stdio.h>

                                int isPalindrome(int num)
                                {
                                    // reversing a number logic - dividing % it by 10
                                    int reversed = 0;
                                    int orignialNumber = num;
                                    while (num != 0)
                                    {
                                        reversed = reversed * 10 + num % 10;
                                        num = num / 10;
                                    }
                                    if (orignialNumber == reversed)
                                    {
                                        return 1;
                                    }
                                    else
                                    {
                                        return 0;
                                    }
                                }
                                
                                int main()
                                {
                                    int number;
                                    printf("Enter a number to check whether it is a palindrome or not\n");
                                    scanf("%d", &number);
                                
                                    if (isPalindrome(number))
                                    {
                                        printf("This number is a palindrome\n");
                                    }
                                    else
                                    {
                                        printf("This number is not a palinddrome\n");
                                    }
                                    return 0;
                                }