C Program to Check whether a number is Armstrong or Not

C program to check whether a number is armstrong number: The following C program checks whether input number is armstrong or not.

Example of Armstrong number: 153 = 1*1*1 + 5*5*5 + 3*3*3

Algorithm to check armstrong number

  1. Accept a number.
  2. Count the number of digits in a number.
  3. Calculate the digit raised to number of digits, that is power(digit,number of digits) for each digit.
  4. Sum up all these calculations.
  5. If the number obtained by summing up is equal to the original input number then it is a armstrong number.

C Program to check Armstrong number

Here is the source code of C program for armstrong number
/* C program to check whether the number is Armstrong number or not */

#include<stdio.h>
#include<math.h>

int main()
{ 
	int temp,count,original,num,sum=0,r;

	printf("\n Enter any number:- ");
	scanf("%d",&num);

	temp=num;

	while(num>0)
	{
	r=num%10;
	num/=10;
	count++;
	}
 
	original=temp;

	while(temp>0)
	{
	r=temp%10;
	sum=sum+pow(r,count);
	temp/=10;
	}

	if (original==sum)
	{
	printf("\n %d is a Armstrong number \n \n",original);
	}
	else
	{
	printf("\n %d is not a Armstrong number \n \n",original);
	}

	return 0;
}

/* Output of above code:- 

 Enter any number:- 153

 153 is a Armstrong number

*/

C program to check whether given number is amrstrong or not using function

/* Aim: Write a program for the following function prototype
void isArmstrong(int n)*/


#include<stdio.h>

void isArmstrong(int n); // isArmstrong Function Prototype

void main()
{
	int n;

	printf("\n Enter any number:- ");
	scanf("%d",&n);

	isArmstrong(n);
}

// isArmstrong Function
void isArmstrong(int n)
{

	int r,sum=0,original;

	original=n;

	while(n>0)
	{
	r=n%10;
	sum=sum+r*r*r; n=n/10; 
	}
	if(original==sum)
		printf("\n %d is Armstrong number \n \n",original); 
	else
		printf("\n %d is not Armstrong number \n \n",original);

}

Output

 Enter any number:- 153

 153 is Armstrong number