031. Write a program to accept a number from user and check it it is Armstrong number or not i.e. 153 = 1^3 + 5^3 + 3^3 = 153 in C language

#include
#include
main()
{
  int temp,rem,sum=0,n;
  clrscr();
  printf("Enter n : ");
  scanf("%d", &n);
  temp=n;
  while(n > 0)
  {
	   rem=n%10;
	   sum=sum+rem*rem*rem;
	   n=n/10;
  }
  if(temp==sum)
		printf("Armstrong Number");
  else
		printf("Not an Armstrong Number");
  getch();
}

One thought on “031. Write a program to accept a number from user and check it it is Armstrong number or not i.e. 153 = 1^3 + 5^3 + 3^3 = 153 in C language

  1. Here is the extended version of the program.. to check Armstrong number conditions for the number of any given length


    int getMultiplication(int rem, int intLength)
    {
    int sum = 1;
    for (int i = 1; i< = intLength; i++) { sum *= rem; } return sum; } int findLength(int n) { int count=0; while(n > 0)
    {
    count = count + 1;
    n=n/10;
    }
    return count;
    }

    void main()
    {
    int temp,rem,sum=0,n, intLength;

    printf("Enter n : ");
    scanf("%d", &n);
    temp=n;
    intLength = findLength(n);
    printf(" intLength= %d ",intLength);

    while(n > 0)
    {
    rem=n%10;
    sum = sum+ getMultiplication(rem, intLength );
    n=n/10;
    }
    if(temp==sum)
    printf("Armstrong Number");
    else
    printf("Not an Armstrong Number");

    }

Leave a Reply

Your email address will not be published. Required fields are marked *