What is Armstrong Number in C?
An Armstrong number (also known as a narcissistic number, plenary number, or pluperfect number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because:
It’s named after Michael F. Armstrong, who used it as an example in a programming problem in 1969. These numbers are intriguing in mathematics and programming because they are relatively rare and often used in exercises to test programming skills.
Armstrong Number Example
If the Armstrong number is a positive integer of order n then it can be defined as,
abcde…. = pow (a, n) + pow (b, n) + pow (c, n) + pow (d, n) + pow (e, n) + ………
For example : 0, 1, 153, 370, 371, 1634 etc.
Let’s check whether 370 is an Armstrong number or not
370 = (3 * 3 * 3) + (7 * 7 * 7) + (0 * 0 * 0)
Here,
(3 * 3 * 3) = 27
(7 * 7 * 7) = 343
(0 * 0 * 0) = 0
27 + 343 + 0 = 370, which is equal to the given number; hence it is an Armstrong number.
Let’s check four digits Armstrong number:
1634 = (1 * 1 * 1 * 1) + (6 * 6 * 6 * 6) + (3 * 3 * 3 * 3) + (4 * 4 * 4 * 4)
Here,
(1 * 1 * 1 * 1) = 1
(6 * 6* 6 * 6) = 1296
(3 * 3 * 3 * 3) = 81
(4 * 4 * 4 * 4) = 256
1 + 1296 + 81 + 256 = 1634, which is equal to the given number; hence it is an Armstrong number.
Algorithm of Armstrong Number in C
- Take input from the user
- Initialize sum = 0 and take temporary variable to temporarily store user input (var = num)
- Now find out the total number of digits in the given number
- Total number of digits get stored in a
- Repeat the loop till var > 0
- Store the output of while loop in sum
- Check whether the user number is equal to sum or not
- If it is equal than print “It is an Armstrong number”
- Else print “It is not an Armstrong number”
Program of Armstrong Number in C
#include
#include
int main ()
{
int num, var, rem, sum = 0, a = 0 ;
printf ( “ Please enter an integer: “ ); // Taking the user input
scanf ( “%d”, &num );
var = num;
while (var != 0) // Finding the numbers of digits in a given number
{
var = var / 10;
++a;
}
var = num;
while (var > 0 ) // Calculate the number to check it is Armstrong or not
{
rem = var % 10;
sum = sum + pow( rem, a );
var = var / 10;
}
if ( sum == num ) // Check whether the sum is equal to the given number of not
{
printf ( “ %d is an Armstrong number n ”, num );
}
else
{
printf ( “ %d is not an Armstrong number n ”, num );
}
return 0;
}
Output 1:
Please enter an integer: 371
371 is an Armstrong number
Output 2:
Please enter an integer: 1045
1045 is an Armstrong number
Explanation
In the above code, we have first declared all the variables that are needed in the program. The num is declared to hold the user input. The var is used for the temporary storage, and then rem is used to hold the remainder that is required for calculation. At last, we have the sum and a, which are assigned to zero.
We are accepting user input and then assigning that number to num. Then we assign this number to var so that we can make our calculation and keep the user input safe in the num variable.
Now we are calculating the number of digits that user input has. For that, we have assigned num value to var, and we have taken a while loop. The while loop will be running until (var !=0) and then divide var by ten so that we can count the total number of digits in that number. Since var is of integer type, it will not store a decimal number, and then each time, the value of an increases by 1. This process is repeated until var is equal to zero. After that, it exits the loop.
Now again, we assign num value to var. Then we start the loop, and this loop will run till var is greater than zero. We have three steps to perform inside the loop. First, we find the remainder of the number, and then we calculate the power of the remainder using pow(rem, a), where rem represents the remainder, and a represents the power, i.e., total number of digits in a number, which was calculated above. Now we divide the var by 10, and this is done because we don’t require the last digit of the number because we have already used it. Since var is an integer type, it neglects the decimal value and stores the integer value. This process is repeated till var is greater than zero; after that, it exits from the loop, and the final value is stored in sum.
Once the control moves out of the loop, the if statement checks whether sum is equal to the num or not, where the num is the input given by the user. If the sum is equal to num, then it is an Armstrong number. Else it is not an Armstrong number.
So, here is all about “Armstrong Number in C.” We hope you find this implementation informative. Stay tuned for more upcoming blogs. Take up the Free Online C Programming for Beginners Course and learn more such concepts!
Source link
#Armstrong #Number #Examples #Programs
Unlock the potential of cutting-edge AI solutions with our comprehensive offerings. As a leading provider in the AI landscape, we harness the power of artificial intelligence to revolutionize industries. From machine learning and data analytics to natural language processing and computer vision, our AI solutions are designed to enhance efficiency and drive innovation. Explore the limitless possibilities of AI-driven insights and automation that propel your business forward. With a commitment to staying at the forefront of the rapidly evolving AI market, we deliver tailored solutions that meet your specific needs. Join us on the forefront of technological advancement, and let AI redefine the way you operate and succeed in a competitive landscape. Embrace the future with AI excellence, where possibilities are limitless, and competition is surpassed.