Tuesday, 24 July 2018

Palindrome in C programming language as number and as string

What is palindrome?

Any word , phrase or sequence that reads the same backward and as forward .

Example ::  madam , 22 , 33 , nurse etc.

Program to check palindrome number in C.

#include <stdio.h>
void main ()
{
int n , opposite = 0 , temp ;
clrscr () ;
printf ("\n Enter a number:") :
scanf ("%d", & n) ;
temp = n ;
while ( temp != 0 )
{
opposite = opposite * 10 ;
opposite = opposite + temp%10 ;
temp = temp / 10 ;
}
if ( n == opposite )
printf ("\n Number is palindrome:") ;
else
printf ("\n Not a palindrome:") ;
getch () ;

Palindrome of sting using string functions ::

#include<stdio.h>
#include<string.h>
void main ()
{
char x [50] , y[50] ;
printf ("\n Enter a string:") ;
gets (x) ;
strcpy ( y , x ) ;
strrev ( y ) ;
if ( strcmp ( x , y ) == 0 )
{
printf ("\n Enter string is palindrome:") ;
else
printf ("\n Not a palindrome:") ;
getch () ;
}

palindrome of string without string function ::

#include <stdio.h>
#include <string.h>
void main ()
{
char word [50] ;
int first , second , last, size = 0 ;
clrscr () ;
printf ("\n Enter a word:") ;
gets ( word ) ;
while ( word [ size ] ! = 0 ) 
size ++ ;
last = size - 1 ;
second = size / 2 ;
for ( first = 0 ; first < second : first++ )
{
if ( word [ first ] != word [ last ] ) 
{
printf ("\n Not a palindrome:") ;
break ;
}
last -- ;
}
if ( first == second )
printf ("\n palindrome:") ;
getch () ;
}






No comments:

Post a Comment

Featured post

Program to find maximum of three number using user define function in C++

A function can be define by the user as per the requirement of the user or programmer . To implement a user define function , a programmer ...