Program to Check whether a Number is Pallindrome
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, s=0;
clrscr();
printf("Enter a number:\t");
scanf("%d", &a);
c = a;
//the number is reversed inside the while loop.
while(a > 0)
{
b = a%10;
s = (s*10)+b;
a = a/10;
}
//Here the reversed number is compared with the given number
if(s == c)
{
printf("The number %d is a pallindrome", c);
}
else {
printf("The number %d is not a pallindrome", c);
}
getch();
}
Output
Enter a numbers 121 The number 121 is a pallindrome
Program to Check whether a Number is Pallindrome or not using Recursion
#include<stdio.h>
#include<conio.h>
void checkPallindrome(int num);
static int p;
void main()
{
int num;
clrscr();
printf("Enter a number:\t");
scanf("%d", &num);
p = num;
checkPallindrome(num);
getch();
}
int b;
static int s = 0;
void checkPallindrome(int num)
{
static int c;
c = num;
b = num%10;
s = (s*10)+b;
num = num/10;
if(num>0)
{
checkPallindrome(num);
}
else
{
if(s == p)
{
printf("The number %d is a pallindrome", p);
}
else {
printf("The number %d is not a pallindrome", p);
}
}
}
Output
Enter a numbers 143 The number 143 is not a pallindrome
No comments:
Post a Comment