C

Armstrong Number in C

In this tutorial, we are going to see how to write a C program to check Armstrong number. A positive number is called an Armstrong number if it is equal to the sum of the cubes of its digits, e.g. 0, 1, 153, 370, 371, 407, etc.

That is to say, the following equation will be verified.

xy..z = xn + yn + ….. + zn

n is the number of digits.

For example, 370 is a 3-digit Armstrong number.

370 = 33 + 73 + 03
        = 27 + 343 + 0
        = 370

Now we will see the implementation of the Armstrong number in C.
 

 

C Program to Check Armstrong Number
#include<stdio.h>

int main()  
{ 
  int nbr, a, tmp, sum=0;  
  
  printf(" Enter a number: ");
  scanf("%d", &nbr);  
  
  tmp=nbr;  
  
  while(nbr>0)  
  { 
    a=nbr%10;  
    sum=sum+(a*a*a);  
    nbr=nbr/10;  
  }  
  if(tmp==sum)  
    printf(" %d is an Armstrong number",tmp);  
  else  
    printf(" %d is not an Armstrong number",tmp);  
  return 0;
}

Output:

Enter a number: 370
370 is an Armstrong number
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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