c programming for Armstrong number 3 Ways

c programming for Armstrong number

Introduce c programming for Armstrong number

Welcome to the enthralling realm of Armstrong numbers in C! As a coding aficionado eager to harness the power of programming, you’re in the perfect spot. This article will dive into the concept of Armstrong numbers and their implementation in the C programming language. An Armstrong number, also termed a narcissistic number, is one that equals the sum of its digits each raised to the power of the count of digits. This intriguing mathematical notion exemplifies the beauty and sophistication of number theory. Grasping and coding Armstrong numbers in C not only sharpens your programming acumen but also deepens your appreciation for the complexities of numerical manipulation.

What are Armstrong number? C programming

An Armstrong number is a positive integer in which the sum of its own digits, each raised to the power of the number of digits, equals the number itself. For example, 153 is an Armstrong number because (1^3 + 5^3 + 3^3 = 153). In C programming, you can check for an Armstrong number by using loops and the modulus operator to raise and sum the digits to the appropriate power.

Understanding the concept of Armstrong numbers

An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits in the number. For example, the number 153 is an Armstrong number because (1^3 + 5^3 + 3^3 = 153).

Mathematical Expression of Armstrong numbers

ABC…N = An + Bn + Cn + … + Nn

For example, Armstrong numbers

370 is an Armstrong number because (3^3 + 7^3 + 0^3 = 370). Armstrong numbers are also known as narcissistic numbers or pluperfect numbers.

List of Armstrong numbers 1 to1000

In the range from 1 to 1000, there are 12 Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, and 371. An Armstrong number, also known as a narcissistic number, is a number that equals the sum of its own digits each raised to the power of the number of digits.

Writing a C program to check for Armstrong numbers Any input Digit

Armstrong Program in c

// Function to count the number of digits in a number
int countDigits(int num) {
int count = 0;
while (num != 0) {
num /= 10;
count++;
}
return count;
}

// Function to check if a number is an c programming for Armstrong number
int isArmstrong(int num) {
int originalNum = num;
int remainder, result = 0;
int n = countDigits(num);
while (originalNum != 0) {
    remainder = originalNum % 10;
    result += pow(remainder, n);
    originalNum /= 10;
}

return result == num;
}

int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);

if (isArmstrong(number))
    printf("%d is an c programming for Armstrong number.\n", number);
else
    printf("%d is not c programming for Armstrong number.\n", number);

return 0;

}

Armstrong number in C Programming with logic explanation

  1. Counting Digits (countDigits function):
    • We need to find the number of digits in the given number. This is important because we’ll use this count to determine the power to which each digit is raised in the Armstrong number check.
    • The countDigits function iterates through the number, dividing it by 10 until it becomes zero. Each time we divide, we increment a counter variable. This gives us the count of digits.
  2. Armstrong Number Check (isArmstrong function):
    • We take the original number and store it because we’ll need it for comparison later.
    • We calculate the number of digits (n) using the countDigits function.
    • We then loop through each digit of the original number:
      • Take the remainder of the original number when divided by 10. This gives us the last digit.
      • Raise this digit to the power of n (number of digits) and add it to the result.
      • Remove the last digit from the original number by integer division.
    • After the loop, if the result equals the original number, then it’s an Armstrong number, else it’s not.
  3. Main Function:
    • Takes input from the user, the number to be checked.
    • Calls the isArmstrong function with the input number.
    • Prints whether the number is an Armstrong number or not based on the result returned by isArmstrong function.

Conclusion: Becoming a master in C programming for Armstrong numbers

Websites to Learn the C Programming Language

  1. C Language Syllabus pdf 2024 – Programming Hack
  2. Checking Prime Number C Programming-Loop, if-else, break 2024 – Programming Hack
  3. Debugging C Output: A Guide to Understanding and Predicting Program Output
  4. 6. Basics of C Programming MCQ Boost Your Skills with Targeted Questions ( introduction C Programming)
  5.  Integer Array in C – How to Declare Int Arrays with C Programming (freecodecamp.org)
  6.  Pass arrays to a function in C (programiz.com)

1 thought on “c programming for Armstrong number 3 Ways”

Leave a Comment