C program to find armstrong number

C program to find armstrong number

279
5
SHARE

C program to find Armstrong number : Armstrong are those numbers in which sum of cubes of individual digits is equal to the number. For example 371 = 33 + 73 + 13 = 27 + 343 +1 =371. Another example of Armstrong number is 153 =  13 + 53 + 33 = 1+ 125 + 27 = 153.  Now how we can achieve the same logic using .

First of all we need to break the each number into individual digits, this can be achieved by remainder logic i.e. modulus (%) in . Now we need to iterate the loop for each digit to achieve the required goal.

Lets see how to write a .

C program to find Armstrong number using for loop :

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

main()
{
   int r;
   long number = 0, c, sum = 0, temp;

   printf("Enter the maximum range upto which you want to find armstrong numbers ");
   scanf("%ld",&number);

   printf("Following armstrong numbers are found from 1 to %ld\n",number);

   for( c = 1 ; c <= number ; c++ )
   {
      temp = c;
      while( temp != 0 )
      {
         r = temp%10;
         sum = sum + r*r*r;
         temp = temp/10;
      }
      if ( c == sum )
         printf("%ld\n", c);
      sum = 0;
   }

   getch();
   return 0;
}

The above C program will generate the Armstrong number between range of numbers. Now you just want to check that input number is Armstrong number or not then we need to manipulate the if (c == sum) part of the above program. Lets see C program to check number is Armstrong number or not.

C program to check number is Armstrong number or not using for loop :

#include <stdio.h>

main()
{
   int number, sum = 0, temp, remainder;

   printf("Enter a number\n");      
   scanf("%d",&number);

   temp = number;

   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10; 
   }

   if ( number == sum )
      printf("Entered number is an Armstrong number.");
   else
      printf("Entered number is not an Armstrong number.");         

   return 0;
}

I hope you all have enjoyed the above code. If you have any doubts or queries regarding above program then ask in form of comments below.

5 COMMENTS

  1. I like the way U explain the logic of the program. In fact it’s useful for the beginners like me to understand the logic behind the program presented in such a simple manner. Doing Good Job.
    Thumbs Up!

  2. C program to find Armstrong number using for loop :
    for( c = 1 ; c <= number ; c++ )

    This part can be slightly optimized, since you only need to check 1..min(number, 10^4). Since there are no Armstrong numbers above 10^4, it can be checked by equation (9^3)*k <= 10^k.

  3. temp = c;
    while( temp != 0 )
    {
    r = temp%10;
    sum = sum + r*r*r;
    temp = temp/10;
    }

    i didnt undestend …plz tell r=temp%10 and sum=sum+r*r*r and temp= temp/10

  4. The definition you used for armstrong number is incorrect.If there are 4 digits in a number,it will become an armstrong number if it is equal to the sum of fourth powers of digits.This continues..

LEAVE A REPLY