C Program To Check for armstrong, palindrome, & perfect number in a single program

Leave a Comment
#include<stdio.h>
#include<conio.h>
int main()
{
int n,c,r,s,t,i;
do{
printf("\nEnter the choice!\n");
printf("\nEnter 1 for checking Armgstrong number!\nEnter 2 for checking perfect square number!\nEnter 3 for checking perfect number!\nEnter 0 to exit!\n");
printf("\nEnter your choice:");
scanf("%d", &c);
if(c==0)
{
printf("\nClosing the program!!");
getche();
exit(0);
}
printf("\nEnter the number for checking:");
scanf("%d", &n);
switch(c)
{
case 1:{
t=n;s=0;
while(t!=0)
{
r=t%10;
t=t/10;
s=s+r*r*r;
}
if(s==n)
printf("\nEntered number is an Armgstrong number!");
else
printf("\nEntered number is not an Armgstrong number!");
break;
}
case 2:{
t=0;
for(i=1;i<=n;i++)
{
if((i*i)==n)
{
t=1;
break;
}
}
if(t==1)
printf("\nThe number is a perfect square!");
else
printf("\nThe number is a not perfect square!");
break;
}
case 3:{ s=0;
for(i=1;i<=n/2;i++)
{
if(n%i==0)
{
s=s+i;
}
}
if(s==n)
printf("\nThe number is perfect!!");
else
printf("\nThe number is not perfect!!");
break;
}
default: printf("\nWrong choice entered!!");
}
}while(n!=0);
getche();
return 0;
}

0 comments: