What is an armstrong number ?
A number n is said to be an armstrong number of order o (o is number of digits in the number n) if it meets following condition.
d1d2d3d4d5d6d7 d....do = d1o + d2o + d3o + d4o + d5o + d6o + .... doo = n
where d1,d2,d3,d4,d1, ... are digits in a number.
o is number of digits in a number.
n is the number that needs to be checked if it is an armstrong number.
Example :
1 = 11 = 1
2 = 21 = 2
.
9 = 21 = 9
153 = 13 + 53 + 33 = 1 + 125 + 27 = 153
371 = 33 + 73 + 13 = 27 + 343 + 1 = 371
.
1634 = 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634
Algorithm :
The steps involved in finding is a number is an armstrong or not are as follows :
1. Find the number of digits of the number - this will be our order, say O.
2. Take the last digit.
3. Calculate power O of this digit - use any function from here.
4. Add this to some predeclared sum.
5. Remove the last digit from the number.
6. Repeat steps 2 to 5 until number becomes 0.
7. Compare sum and the number - if both are same, it is armstrong number.
where d1,d2,d3,d4,d1, ... are digits in a number.
o is number of digits in a number.
n is the number that needs to be checked if it is an armstrong number.
Example :
1 = 11 = 1
2 = 21 = 2
.
9 = 21 = 9
153 = 13 + 53 + 33 = 1 + 125 + 27 = 153
371 = 33 + 73 + 13 = 27 + 343 + 1 = 371
.
1634 = 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634
Algorithm :
The steps involved in finding is a number is an armstrong or not are as follows :
1. Find the number of digits of the number - this will be our order, say O.
2. Take the last digit.
3. Calculate power O of this digit - use any function from here.
4. Add this to some predeclared sum.
5. Remove the last digit from the number.
6. Repeat steps 2 to 5 until number becomes 0.
7. Compare sum and the number - if both are same, it is armstrong number.
/*
* C Program to check if a number is armstrong or not.
*/
// Includes
#include <stdio.h>
// Declarations
/* isArmstrong
* Description : A function to check if given number is armstrong or not.
* Parameters : i_number is a number to be checked for armstrong.
* Returns : unsigned short 0 - for non armstrong number,
1 - for armstrong number.
*/
unsigned short isArmstrong(int i_number);
// Definitions
int main()
{
int i_number;
printf("Enter number to be checkd for armstrong : ");
scanf("%d",&i_number);
// Check if the number is armstrong
if (isArmstrong(i_number))
{
printf("Entered number %d is armstrong.",i_number);
}
else
{
printf("Entered number %d is not armstrong.",i_number);
}
}
unsigned short isArmstrong(int i_number)
{
// numbers copy, so that when we remove last digit every time,
// we don't loose the original number.
int i_numberCopy = i_number;
// stores the sum of powers if digits of a number.
unsigned int sum = 0;
unsigned int i_numberOfDigits;
unsigned short us_lastDigit;
// Negative numbers are not armstrong numbers.
if (i_number > 0)
{
i_numberOfDigits = getNumberOfDigits(i_number);
if (i_numberOfDigits > 0)
{
while (0 != i_numberCopy)
{
// Take last digit
us_lastDigit = i_numberCopy%10;
// Calculate the power and add it in sum
sum += bitwisePower(us_lastDigit, i_numberOfDigits);
// remove the last digit
i_numberCopy /= 10;
}
// Check if number is same as that of sum of powers of digit.
if (i_number == sum)
{
return 1;
}
}
}
return 0 ;
}
No comments:
Post a Comment