- 0
Notifications Mark All Read
- Login
- Get Prime
Armstrong number in C
What is an Armstrong number?
An Armstrong number in C or in any language is a number that is equal to the sum of digits that is raised to the power of number of digits in the number. In simple terms if a three digit number is Armstrong number only if the sum of individual’s cube is equal to the original number itself.
Syntax:
abcd... = an + bn + cn + dn +
Syntax:
254 = 2*2*2 + 5*5*5 + 4*4*4
Algorithm:
- Step 1: Start
- Step 2: Initialize and declare the variable current_pointer, sum=0 and num=number.
- Step 3: Read 3 digit integer input from the user.
- Step 4:Do step 5 to 7 until num>0.
- Step5:current_pointer=(num%10)
- Step 6: sum=sum(current_pointer*current_pointer*current_pointer)
- Step 7: num=number/10
- Step 8: Check if the sum==number
- Step 9: If true then print”The number is an Armstrong number”.
- Step 10: Else print” Number is not an Armstrong number”.
- Step 11: End.
Working:
Example 1 to check Armstrong number in C:
Program where the number is predefined and checked if it is Armstrong number or not.
#include<stdio.h> #include<math.h> int isArmstrong(int number) { int current_digit, sum = 0, num = number, number_of_digits; while (num > 0) { current_digit = num % 10; sum = sum + pow(current_digit, 3); num = num / 10; } if (sum == number) { return 1; } else { return 0; } } int main() { int n= 371, isArmstrongNumber; isArmstrongNumber = isArmstrong(n); if (isArmstrongNumber == 1) { printf("%d is an Armstrong Number.", n); } else { printf("%d is not an Armstrong Number.", n); } return 0; }
Output:
371 is an Armstrong Number.
Example 2 to check Armstrong number in C:
Program to check a three digit number taken as input from the user to check if it is Armstrong number or not.
#include<stdio.h> int main() { int n, Num, rem, result = 0; printf("Enter a three-digit integer: "); scanf("%d", &n); Num = n; while (Num != 0) { rem = Num % 10; result += rem * rem * rem; Num /= 10; } if (result == n) printf("%d is an Armstrong number.", n); else printf("%d is not an Armstrong number.", n); return 0; }
Input:
Enter a three-digit integer: 371
Output:
371 is an Armstrong number.
Example 3 to check Armstrong number in C:
Program to check a number taken as input from the user to check if it is Armstrong number or not.
#include<stdio.h> #include<math.h> int main() { int n, Num, remainder, i = 0; float result = 0.0; printf("Enter an integer: "); scanf("%d", &n); Num = n; for (Num = n; Num != 0; ++i) { Num /= 10; } for (Num = n; Num != 0; Num /= 10) { remainder = Num % 10; result += pow(remainder, i); } if ((int)result == n) printf("%d is an Armstrong number.", n); else printf("%d is not an Armstrong number.", n); return 0; }
Input:
Enter a three-digit integer: 1634
Output:
1634 is an Armstrong number.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment