Tuesday 7 October 2014

Write a Program to check a number it is Armstrong or not by using while and if Statement


Armstrong Number

Armstrong Number:
  • Three Digit Armstrong Number defined as the Sum of cubes of its Digit is equal to the Number Itself.
  • Armstrong Number is also known as Narcissistic Number.
  • Example: 153 = (1*1*1) + (5*5*5) + (3*3*3)
Statement of C Program: Enter a number and check it is Armstrong or not:

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b,c,d;
clrscr();
printf(" Enter a number");
scanf("%d" ,&a);
d=a;
c=0;
while(a>0)
{
b=a%10;
c=c+(b*b*b);
a=a/10;
}
if(c==d)
{
printf(" Number is Armstrong");
}
else
{
printf(" Number is not Armstrong");
}
getch();
}
Output:
Enter a number
153
Number is Armstrong


Output:
Enter a number
180
Number is not Armstrong

No comments:

Post a Comment