How to check if a number is a palindrome using for loop






#include<stdio.h>
int main(){
    int num,r,sum=0,temp;

    printf("Enter a number: ");
    scanf("%d",&num);

    for(temp=num;num!=0;num=num/10){
         r=num%10;
         sum=sum*10+r;
    }
    if(temp==sum)
         printf("%d is a palindrome",temp);
    else
         printf("%d is not a palindrome",temp);

    return 0;
}

Sample output:
Enter a number: 1221
1221 is a palindrome



Algorithm:

Definition of Palindrome number or What is palindrome number?

A palindrome number is one that remains unchanged when its digits are reversed. For instance, 121 is a palindrome number because, when its digits are reversed, it still reads as 121.

Palindrome numbers examples: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191 etc. 



No comments: