Palindrome Program
C PROGRAM TO DETERMINE PALINDROME NUMBER
Back To Palindrome Program
#include<stdio.h>
int main()
{
// variable for a=number, n=to copy number, d=digit, rev=reverse of number
int a,n,d=0,rev=0;
// Inputting number
printf("Enter any number to determine whether it is Palindrome or not:");
scanf("%d",&a);
// copying number in n
n=a;
// Determining whether number is palindrome or not
while(n!=0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
// Displaying Whether number is palindrome or not
if(a==rev)
printf("Palindrome number");
else
printf("Not a Palindrome Number");
return 0;
}
C PROGRAM TO PRINT(LIST) PALINDROME NUMBER FROM 10 TO n
Back To Palindrome Program
#include<stdio.h>
int main()
{
/*declaring variable for num=user wish, i=acts as number, n=to copy number,
d=digit, rev=reverse of number */
int num,n,d=0,rev,i;
// Inputting number
printf("Enter number till which you want to list Palindrome Number:");
scanf("%d",&num);
printf("List of Palindrome Number:");
for(i=10;i<=num;i++)
{
// copying number in n
n=i;
rev=0;
// Determining whether number is palindrome or not
while(n!=0)
{
d=n%10;
rev=rev*10+d;
n=n/10
}
// Displaying Whether number is palindrome or not
if(i==rev)
printf("%d ",i);
}
return 0;
}
C PROGRAM TO FIND WHETHER A NUMBER IS PALINDROME OR NOT USING RECURSION
Back To Palindrome Program
#include<stdio.h> // Declaring global variable r=reverse, d=digit int r=0, d=0; // Defining function with parameter n = number int rev(int n) { /* Base condition : Any condition where a recursive function or method does not invoke itself. */ if(n==0) return r; // Continue calling function rev or function invoke itself else { // Extracting digit d=n%10; // Finding reverse r=(r*10+d); // function invoke itself rev(n/10); } } int main() { // Declaring variable n = number int n; // Declaring variable "r" to hold the reverse number int r; // Inputting Number printf("Enter the Number : "); scanf("%d",&n); // Calling function "rev" with actual parameter "n" passed to it r=rev(n); // Checking and Displaying if a Number is palindrome or Not if(r==n) printf("%d is a Palindrome Number ",n); else printf("%d is not a Palindrome Number ",n);
return 0;
}
C PROGRAM TO ENTER A WORD AND CHECK WHETHER IT IS PALINDROME OR NOT USING STRING FUNCTION
Back To Palindrome Program
// palindrom - When word = reverse of word then it is called palindrome
// Example : madam - palindrome, Madam - not palindrome, MadaM - palindrome
#include<stdio.h>
#include<string.h>
int main()
{
// Declaring variable str=string
char word[50], word1[50];
// Inputing string
printf("Enter any word : ");
scanf("%s",&word);
// Copying word to word1
strcpy(word1,word);
// checking if palindrome or not
if(strcmp(word,strrev(word1))==0)
{
printf("Entered word is a palindrome ");
}
else
printf("Entered word is not palindrome ");
return 0;
}
PALINDROME PROGRAM IN C WITHOUT USING STRING FUNCTION
Back To Palindrome Program
#include<stdio.h>
int main()
{
// Declaring variable str=string
char word[50], revword[50];
/* Declaring variable i=to iterate loop, l=length,
c=count the number of matched character */
int i, j, l=0, c;
// Inputing string
printf("Enter any word : ");
scanf("%s",&word);
// finding length
while(word[l]!='\0')
l++;
// Reversing string
j=0;
for(i=l-1;i>=0;i--)
{
revword[j]=word[i];
j++;
}
revword[j]='\0';
//checking if palindrome or not
c=0;
for(i=0;word[i]!='\0';i++)
{
if(revword[i]==word[i])
c++;
}
if(c==l)
{
printf("Word is a palindrome ");
}
else
printf("Word is not palindrome ");
return 0;
}