Problem: Write a program in C to check whether the given positive number is Armstrong or not.
Armstrong Number is a number whose sum of ‘powered digits with the “total number of digits in the number” ‘ is equal to the original number.
Example 1: Check Armstrong for three Digits Number
- Take positive integer input into original_number
- Copy the original_number to the number
- Run while loop until number != 0
- Extract digit by dividing the number by 10( Remainder is the digit)
- Add cube of digit into sum as sum = sum + (digit*digit*digit)
- Update the number = number/10 (Getting rid of the last digit which has been used as the remainder in step 5)
- End loop
- Check if the sum == original_number
- If YES the Armstrong Number else NOT.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number, original_number, sum=0;
int digit;
printf("Enter a number \n");
scanf("%d",&original_number);
number = original_number; //Copying the original number
while(number != 0){
digit = number % 10;
sum += digit * digit *digit;
number=number/10;
}
if(sum == original_number)
printf("Armstrong Number \n");
else
printf("Not an Armstrong Number");
return 0;
}
Output
When the input is 121 which is not an Armstrong Number.
When the input is 153 which is an Armstrong Number.
Example 2: Check Armstrong for N digits Number
- Take a number as input.
- Copy the original number into temporary variable i.e
temp=num
. - Using the While loop counts the number of digits in temp and store it as length. Update temp =
temp / 10
untiltemp != 0
. - Reinitialize the
temp
variable with the original number. - Using another while loop with the same condition, extract each digit as
digit = temp%10
and update the sum assum = sum + digit^length
. - Outside loop, check if
sum==originalnumber
. If yes then output “Armstrong Number, else “Not Armstrong number”.
#include <stdio.h>
#include <math.h>
int main(){
int num, digit, sum = 0, length = 0;
int temp;
printf("Enter a number \n");
scanf("%d",&num);
temp = num; //Copying the original number for operation on it
//Counting how many digits do temp (i.e num) has
while(temp != 0){
length++;
temp = temp / 10;
}
//On counting digits, the value of temp becomes 0
//So we again assign the fresh copy to it
temp = num;
while(temp != 0){
digit = temp % 10;
sum += floor(pow(digit,length));
temp = temp / 10;
}
//Output Result
if(sum == num)
printf("Armstrong number \n");
else
printf("Not an Armstrong number \n");
return 0;
}
Output
Checking 4 digit number.
Example 3: Check Armstrong from 1 to 1000
In this C Program, we will check and print all Armstrong numbers in the range from 1 to 1000.
#include <stdio.h>
#include <math.h>
int main(){
int num, digit, sum = 0;
int i, temp;
for(i=1; i<1000; i++){
num = i;
sum = 0;
temp = num; //Copying the number
while(temp != 0){
digit = temp % 10;
sum += floor(pow(digit,3));
temp = temp / 10;
}
//Output Result
if(sum == num)
printf("%d, ",num);
}
return 0;
}
Output
In this tutorial, we learned to check Armstrong number in C with examples.
why do we copy the original number?
{ temp = num; //Copying the original number for operation on it }
Because while loop is updating the temp variable. If we had used the num (original input number) in the loop, we would lose it and not be able to compare it with the sum value.