Thursday, March 6, 2014

C program to find armstrong number

Share Post:
A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 3^3 + 7^3 + 1^3 = 371.
#include <stdio.h>                                                     //header file
#include<conio.h>                                                    //header file
int main()
{                                                                                 //start of main body
   int number, sum = 0, temp, remainder;


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

   temp = number;

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

   if ( number == sum )
      printf("Entered number is an armstrong number.\n");
   else
      printf("Entered number is not an armstrong number.\n");

   return 0;
}

0 comments:

Post a Comment