Tuesday, 12 April 2011

C prog for Reversing and checking an integer is a palindrome or not


#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include <math.h>
int Palindrome(int);

void main()
{
int a=0,b=0;
long int n=1;
int k;
int  numdigit=5;
//for palindrome

printf("Enter the any  number");
scanf("%d",&a);
k=Palindrome(a);
if(k==1)
{
printf("The given number is a palindrome:\n");
}
else
printf("The given number is not  a palindrome:\n");



_getch();
}

int Palindrome(int a1)
{
int temp=1,remainder=0,temp1=0,a2=0;
int a3=0,count=1,a5=0;
a3=a1;
a5=a1;
for(int i=0;i<100;i++)
{

int a4=a3/10;
a3=a4;

if(a3==0)
{
a2=count;
break;
}
else
count++;
a2=count;
}

int* arr={(int*)malloc(sizeof(int)*a2)};

for(int i=0;i<(a2+1);i++)
{
temp= a1/10;
remainder=a1-temp*10;
*(arr+i)=remainder;
a1=temp;
if (temp==0)
break;
}


for(int i=0;i<(a2+1);i++)
{
temp1+=(*(arr+i))* pow (10.0f,(a2-(i+1)));
if(a2-(i+1)==0)
break;
}

if (temp1==a5)
{
return 1;
}

else

return 0 ;
}

No comments:

Post a Comment