Learn C ProgrammingLearn ProgrammingLoop Control StructuresPrograms On Loop Control Structures While And Do-WhileResources

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

By September 18, 2012 April 26th, 2020 One Comment
#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 Comment

  • 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

DEMO01