Understanding Armstrong numbers is a key concept in programming, particularly in C. This article will walk you through the process of checking if a number is an Armstrong number in C using three distinct methods, complete with detailed explanations, and results.
Write a “Program to check Armstrong number in C”
What is an Armstrong Number?
An Armstrong number for a given number of digits is a number that equals the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because:
153 = 13+53+33 = 1 + 125 + 27 = 153
Method 1) Armstrong number in C using a Simple While Loop
Code Example
#include <stdio.h> #include <math.h> int main() { int num, originalNum, remainder, n = 0, result = 0; printf("Enter an integer: "); scanf("%d", &num); originalNum = num; // Find the number of digits while (originalNum != 0) { n++; originalNum /= 10; } originalNum = num; // Check if the number is an Armstrong number while (originalNum != 0) { remainder = originalNum % 10; result += pow(remainder, n); originalNum /= 10; } if (result == num) { printf("%d is an Armstrong number.\n", num); } else { printf("%d is not an Armstrong number.\n", num); } return 0; }
Explanation of the Code
This method uses a while loop to decompose the number into its digits, raise each digit to the power of the number of digits, and then sum these values.
Results
Input:
Enter an integer: 153
Output:
153 is an Armstrong number.
Method 2) Armstrong number in C using a For Loop and String Manipulation
Code Example
#include <stdio.h> #include <math.h> #include <string.h> int main() { int num, originalNum, result = 0, n; char numStr[10]; printf("Enter an integer: "); scanf("%d", &num); sprintf(numStr, "%d", num); n = strlen(numStr); originalNum = num; for (int i = 0; i < n; i++) { int digit = numStr[i] - '0'; result += pow(digit, n); } if (result == num) { printf("%d is an Armstrong number.\n", num); } else { printf("%d is not an Armstrong number.\n", num); } return 0; }
Explanation of the Code
This method involves converting the number to a string to easily count the digits and then iterating over each character of the string to calculate the sum.
Results
Input:
Enter an integer: 153
Output:
153 is an Armstrong number.
Method 3) Armstrong number in C using Recursion
Code Example
#include <stdio.h> #include <math.h> int numDigits(int num) { int digits = 0; while (num != 0) { num /= 10; digits++; } return digits; } int armstrongSum(int num, int n) { if (num == 0) { return 0; } else { int remainder = num % 10; return pow(remainder, n) + armstrongSum(num / 10, n); } } int main() { int num, n, result; printf("Enter an integer: "); scanf("%d", &num); n = numDigits(num); result = armstrongSum(num, n); if (result == num) { printf("%d is an Armstrong number.\n", num); } else { printf("%d is not an Armstrong number.\n", num); } return 0; }
Explanation of the Code
In this method, a recursive function calculates the sum of the digits each raised to the power of the number of digits.
Results
Input:
Enter an integer: 153
Output:
153 is an Armstrong number.
Each method provided above demonstrates a unique approach to checking if a number is an Armstrong number or not. Whether using a simple while loop, string manipulation, or recursion, the core concept remains the same: summing the digits raised to the power of the number of digits and comparing the result to the original number. By practicing these methods, you can enhance your understanding of fundamental programming concepts and improve your problem-solving skills in C.