Definition:
Armstrong numbers are the sum of their own digits to the power of the number of digits.
In other words
An Armstrong number is an n-digit base b number such that the sum of its (base b ) digits raised to the power n is the number itself.
And according to wikipedia
In recreational number theory, a narcissistic number (also known as a pluperfect digital invariant (PPDI), an Armstrong number or a plus perfect number) is a number that is the sum of its own digits each raised to the power of the number of digits. This definition depends on the base b of the number system used, e.g. b = 10 for the decimal system or b = 2 for the binary system or
For Example:
There are just four numbers, after unity, which are the sums of the cubes of their digits:
- 153 = 13 + 53 + 33
- 370 = 33 + 73 + 03
- 371 = 33 + 73 + 13
- 407 = 43 + 03 + 73.
1.Program to check for Armstrong Number
#include
//Prototype of all the function being used in the program
int number_of_digits(int give_number);int check_armstrong(int given_number,int number_of_digits);int power(int temp_var,int number_of_digits);
//Main function starts
int main(){int give_number,no_of_digits,flag;printf("\nEnter the number you want to check for Armstrong\n");scanf("%d",&give_number);no_of_digits=number_of_digits(give_number);flag=check_armstrong(give_number,no_of_digits);flag?printf("\n%d is Armstron\n",give_number):printf("\n%d is not Armstrong number\n",give_number);return 0;}
//Function to check how many degits are there in the given number
int number_of_digits(int given_number){int count=0,temp;while(given_number>0){temp=given_number%10;given_number=given_number/10;count++;}return count;}
//Function to check that the number is Armstrong or not
int check_armstrong(int given_number,int number_of_digits){int temp_var,clone_number,regenerate_number=0;clone_number = given_number;while(clone_number>0){temp_var=clone_number%10;regenerate_number=regenerate_number + power(temp_var,number_of_digits);clone_number=clone_number/10;}if(regenerate_number==given_number)return 1;elsereturn 0;}
//Function the calculate the power same as pow() function
int power(int temp_var,int number_of_digits){int cal_power=1,i;for(i=0;ical_power=cal_power*temp_var;return cal_power;}
2.Program to find Armstrong Number in a given range
#include
//Prototype of all the function being used in the program
int number_of_digits(long int give_number);int check_armstrong(long int given_number,long int number_of_digits);int power(long int temp_var,long int number_of_digits);
//Main function starts
int main(){long int give_number,no_of_digits,flag;printf("\n Enter the number you want to check\n");for(give_number=1;give_number<9999999;give_number++){no_of_digits=number_of_digits(give_number);flag=check_armstrong(give_number,no_of_digits);flag?printf("\nNumber %ld is Armstron\n",give_number):printf("");return 0;}}
//Function to check how many degits are there in the given number
int number_of_digits(long int given_number){long int count=0,temp;while(given_number>0){temp=given_number%10;given_number=given_number/10;count++;}return count;}
//Function to check that the number is Armstrong or not
int check_armstrong(long int given_number,long int number_of_digits){long int temp_var,clone_number,regenerate_number=0;clone_number = given_number;while(clone_number>0){temp_var=clone_number%10;regenerate_number=regenerate_number + power(temp_var,number_of_digits);clone_number=clone_number/10;}if(regenerate_number==given_number)return 1;elsereturn 0;}
//Function the calculate the power same as pow() function
int power(long int temp_var,long int number_of_digits){long int cal_power=1,i;for(i=0;ical_power=cal_power*temp_var;return cal_power;}
Note: On compiling if you get any error than check it may that html has converted the lass than and greater than symbol as its own tags and also please do proper formatting. This program has been compiled and checked on GCC.
0 comments:
Post a Comment