Tuesday, October 16, 2012

Program To check a Number is Pallindrome & Armstrong


Some Basic C program frequently asked in Interview.

/*To check a number is pallindrome or not */

#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 no. %d is a pallindrome",c);
}
else
printf("The no. %d is not a pallindrome",c);
getch();
}




/*To check a number is armstrong or not */

#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;
//Sum of cube of each digit of given  number is claculated inside the while loop.
while(a>0)
{
b=a%10;
s=s+b*b*b;
a=a/10;
}
//Sum is compared with the given number.
if(s==c)
{
printf("The no. %d is a armstrong no.",c);
}
else
printf("The no. %d is not a armstrong no.",c);
getch();
}

No comments: