Sunday 1 September 2013

Write a c program to check given number is palindrome number or not.



Write a c program to check given number is palindrome number or not.

A Palindrome is a word, phrase or sequence which reads the same in both directions. Derived from the Greek palíndromos, meaning running back again, a palindrome reads the same forward and backward, with general allowances for adjustments to punctuation and word dividers.
Examples of words include: 
EYE, MOM, DAD, NOON, RACECAR, LEVEL, DEED, CIVIC, RADAR, KAYAK (more here)
Examples of number palindromes include:
535
3773
59695
76067
374473
416614
87966978
246191642

Code 1:
1. Wap to check a number is palindrome
2. C program to find whether a number is palindrome or not
#include<stdio.h>
int main(){
    int num,r,sum=0,temp;

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

    temp=num;
    while(num){
         r=num%10;
         num=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: 131
131 is a palindrome

Code 2:
1. Write a c program for palindrome
2. C program to find palindrome of a number
3. Palindrome number in c language
#include<stdio.h>
int main(){
    int num,r,sum,temp;
    int min,max;

    printf("Enter the minimum range: ");
    scanf("%d",&min);

    printf("Enter the maximum range: ");
    scanf("%d",&max);

    printf("Palindrome numbers in given range are: ");
    for(num=min;num<=max;num++){
         temp=num;
         sum=0;

         while(temp){
             r=temp%10;
             temp=temp/10;
             sum=sum*10+r;
         }
         if(num==sum)
             printf("%d ",num);
    }
    return 0;
}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 50
Palindrome numbers in given range are: 1 2 3 4 5 6 7 8 9 11 22 33 44

Code 3:
1. 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

Code 4:
1. C program to check if a number is palindrome using recursion
#include<stdio.h>

int checkPalindrome(int);
int main(){
    int num,sum;

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

    sum = checkPalindrome(num);

    if(num==sum)
         printf("%d is a palindrome",num);
    else
    printf("%d is not a palindrome",num);

    return 0;
}

int checkPalindrome(int num){

    static int sum=0,r;

    if(num!=0){
         r=num%10;
         sum=sum*10+r;
         checkPalindrome(num/10);
    }

    return sum;
}

Sample output:
Enter a number: 25
25 is not a palindrome

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved