Understanding and Implementing Armstrong Numbers in C

Table of Contents

Introduction

An Armstrong number (also known as a narcissistic number, pluperfect digit, pluperfect number, or pluperfect digital invariant) in a given number base is a number that is the sum of its own digits raised to the power of the number of digits. C, a classic and foundational programming language, provides a straightforward approach to identifying such numbers. In this article, we will explore how to determine if a number is an Armstrong number using the C programming language.

What is an Armstrong Number?

An Armstrong number of a given number of digits is an integer such that the sum of its own digits raised to the power of the number of digits is equal to the number itself.

For example:

  • 153 is an Armstrong number because: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

C Implementation

Algorithm:

  1. Take a number as input.
  2. Calculate the number of digits in the number.
  3. For each digit, raise it to the power of the total number of digits and calculate the sum.
  4. Compare the calculated sum with the original number. If they are the same, it’s an Armstrong number.

Code Example:

#include <stdio.h>
#include <math.h>

int main() {
    int num, temp, sum = 0, n = 0;

    printf("Enter a number: ");
    scanf("%d", &num);

    temp = num;

    // Calculate number of digits
    while(temp != 0) {
        temp /= 10;
        ++n;
    }

    temp = num;

    // Calculate sum of nth power of each digit
    while(temp != 0) {
        sum += pow(temp % 10, n);
        temp /= 10;
    }

    if(sum == num)
        printf("%d is an Armstrong number\n", num);
    else
        printf("%d is not an Armstrong number\n", num);

    return 0;
}

Here, two libraries are included:

  • stdio.h: This is the standard input-output library in C. It provides functions to read and write data. In our code, it is used for printing to the console and reading user input.
  • math.h: This library provides mathematical functions. In our code, it is used for the pow() function which calculates the power of a number.
  • num – Stores the number input by the user.
  • temp – Temporary variable used to manipulate and check the value of num without altering it.
  • sum – Stores the sum of the nth power of each digit of the number.
  • n – Counts the number of digits in the number.

This loop runs until temp becomes 0. In each iteration, temp is divided by 10 (which effectively removes its last digit), and the count n is incremented. This gives us the total number of digits in the number.

Here, we reset the value of temp to num to go through its digits again. For each digit (retrieved using temp % 10), we raise it to the power of n using pow() and add it to the sum. Then, we remove the last digit of temp by dividing it by 10. This loop continues until all digits of temp are processed.

We compare the sum to the original num. If they are equal, the number is an Armstrong number. Otherwise, it’s not The return 0; statement signifies successful termination of the program.

Conclusion

Armstrong numbers are a fascinating mathematical concept, and their implementation in C helps novice programmers understand loops, conditional statements, and arithmetic operations in a real-world context. By working on such foundational concepts, one can grasp the intricacies of programming languages like C and cultivate logical thinking and problem-solving skills.

Anand Das

Anand Das

Anand is Co-founder and CTO of Bito. He leads technical strategy and engineering, and is our biggest user! Formerly, Anand was CTO of Eyeota, a data company acquired by Dun & Bradstreet. He is co-founder of PubMatic, where he led the building of an ad exchange system that handles over 1 Trillion bids per day.

From Bito team with

This article is brought to you by Bito – an AI developer assistant.

Latest posts

Effective JavaScript Techniques for Comparing Two Arrays

Mastering Loop Control in Python: Break vs Continue Explained

Reading JSON Files in Python: A Step-by-Step Tutorial

Efficient Data Iteration: Mastering Python Generators

Introduction to Static Variables in Python

Top posts

Effective JavaScript Techniques for Comparing Two Arrays

Mastering Loop Control in Python: Break vs Continue Explained

Reading JSON Files in Python: A Step-by-Step Tutorial

Efficient Data Iteration: Mastering Python Generators

Introduction to Static Variables in Python

Related Articles

Get Bito for IDE of your choice