Armstrong Number In C

1³ + 5³ + 3³ = 1 + 125 + 27 = 153

Formula For Armstrong Number

There exists a standard equation for Armstrong numbers, which can be described as:

Example

Abcd… = pow(a, n) + pow(b, n) + pow(c, n) + pow(d, n) + …

Where a, b, c, and d represent the individual digits of the numerical value, with n denoting the total count of digits present within the specified number.

Different Approaches to Generate Armstrong Number

There are several approaches that are used to generate Armstrong Number in C. Some of them are as follows:

1) Basic Armstrong Number Check (Simple Logic):

In C language, the process of verifying an Armstrong number involves employing loops and if statements, which are core methods of controlling program flow, to establish if a specified number is indeed an Armstrong number. The approach involves utilizing while loops to tally the digits and compute the total by raising each digit to the power of the total count of digits.

This method is particularly beneficial for individuals seeking to grasp the fundamentals of modular division, extracting digits, and performing mathematical calculations as it reinforces the principles of breaking down numbers and making logical choices.

Example code showcasing the fundamental Number checking utilizing a While loop.

Let's consider an illustration to showcase the process of calculating the Armstrong number by employing a while loop in the C programming language.

Example

Example

#include <stdio.h>

#include <math.h>



int main() {   //main function

    int num, originalNum, remainder, result = 0, n = 0;



    // Input from user

    printf("Enter an integer: ");

    scanf("%d", &num);



    originalNum = num;



    // Count the number of digits

    while (originalNum != 0) {

        originalNum /= 10;

        n++;

    }



    originalNum = num;



    // Calculate the sum of nth powers of its digits

    while (originalNum != 0) {

        remainder = originalNum % 10;

        result += pow(remainder, n);

        originalNum /= 10;

    }



    // Check if the number is an Armstrong number

    if (result == num)

        printf("%d is an Armstrong number.\n", num);

    else

        printf("%d is not an Armstrong number.\n", num);



    return 0;

}

Output:

Output

Enter an integer: 236

236 is not an Armstrong number.

Enter an integer: 153

153 is an Armstrong number.

Enter an integer: 9474

9474 is an Armstrong number.

Explanation:

In this illustration, we verify whether a given number is an Armstrong number. Initially, it determines the digit count by employing a while loop. Subsequently, it computes the total sum of each digit raised to the power of the total number of digits using another loop. Ultimately, it validates if the sum matches the original number and then displays a message confirming the number as an Armstrong number.

2) Armstrong Number using Recursion

In the realm of C programming, recursive functions play a key role in computing the Armstrong sum and determining the digit count. Subsequently, a separate recursive function is employed to compute the sum of individual digits raised to the power of the total digit count.

C Example to find Armstrong using Recursion

Let's consider an illustration to showcase the process of verifying an Armstrong number using a recursive function in the C programming language.

Example

Example

#include <stdio.h>

#include <math.h>



// Function to count the number of digits

int countDigits(int num) {

    if (num == 0)

        return 0;

    return 1 + countDigits(num / 10);

}



// Recursive function to calculate the Armstrong sum

int armstrongSum(int num, int power) {

    if (num == 0)

        return 0;

    int digit = num % 10;

    return pow(digit, power) + armstrongSum(num / 10, power);

}



int main() {   //main function

    int num, n, sum;



    // Input from user

    printf("Enter an integer: ");

    scanf("%d", #);



    n = countDigits(num);

    sum = armstrongSum(num, n);



    // Check and print result

    if (sum == num)

        printf("%d is an Armstrong number.\n", num);

    else

        printf("%d is not an Armstrong number.\n", num);



    return 0;

}

</math.h></stdio.h>

Output:

Output

Enter an integer: 370

370 is an Armstrong number.

Enter an integer: 456

456 is not an Armstrong number.

Enter an integer: 407

407 is an Armstrong number.

Explanation

In this instance, we are showcasing the countDigits method which provides a recursive tally of the digits in a number. Subsequently, the armstrongSum function incrementally calculates the sum of each digit raised to a power. Ultimately, the resultant sum is compared with the original number to determine if it qualifies as an Armstrong number.

3) Armstrong Number using For Loop

In C programming, employing a for loop to calculate Armstrong numbers within a specified range (e.g., 1-1000) aids developers in understanding how to iterate through a defined sequence. The utilization of Armstrong logic in the for loop enables the examination of each number as we traverse a series of numbers systematically and comprehensively. This technique proves beneficial in scenarios such as batch processing, where multiple variables undergo identical logical operations.

Below is a C code example that demonstrates how to verify if a number is an Armstrong Number using a for loop:

Example

#include <stdio.h>

int main() {
    int number, originalNumber, remainder, result = 0;

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

    originalNumber = number;

    for (;originalNumber != 0; originalNumber /= 10) {
        remainder = originalNumber % 10;
        result += remainder * remainder * remainder;
    }

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

    return 0;
}

Let's consider an illustration to demonstrate how to identify an Armstrong number using a for loop in the C programming language.

Example

Example

#include <stdio.h>

#include <math.h>



int main() {   //main function

    int num, temp, remain, n, result;



    printf("The Armstrong Numbers which are there from 1 and 1000 are: \n");



    // Loop from 1 to 1000

    for (num = 1; num <= 1000; num++) {

        temp = num;

        n = 0;

        result = 0;



        // Count number of digits

        for (int i = temp; i != 0; i /= 10)

            n++;



        temp = num;



        // Calculate Armstrong sum

        for (int i = temp; i != 0; i /= 10) {

            remain = i % 10;

            result += pow(remain, n);

        }



        // Check if Armstrong

        if (result == num)

            printf("%d ", num);

    }



    return 0;

}

Output:

Output

The Armstrong Numbers which are there from 1 and 1000 are: 

12 3 4 5 6 7 8 9 153 370 371 407

Explanation:

In this instance, we specified the iteration range in the outer for loop as 1-1000. Subsequently, a nested for loop is employed to calculate the number of digits in the current integer. Following this, another nested for loop computes the sum of each digit raised to the power of n. Upon matching the results, the original number is transformed into an Armstrong number.

4) Armstrong Number Using Function:

In C programming, implementing a function to verify an Armstrong number promotes the development of organized, adaptable, and recyclable code. This approach involves segregating the algorithm responsible for determining if a number is an Armstrong number into a custom function. Within this function, an integer is accepted as an argument, and the sum of its digits raised to the power of the total number of digits is computed. The function then evaluates whether this result is equal to the original number. Furthermore, this method facilitates the utilization of the same logic across various inputs or iterations within a loop.

Here is a C code example demonstrating how to verify an Armstrong Number utilizing a Function.

Example

#include <stdio.h>

int checkArmstrong(int num);

int main() {
    int number, result;

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

    result = checkArmstrong(number);

    if (result == 1) {
        printf("%d is an Armstrong number.\n", number);
    } else {
        printf("%d is not an Armstrong number.\n", number);
    }

    return 0;
}

int checkArmstrong(int num) {
    int originalNum, remainder, result = 0;

    originalNum = num;

    while (originalNum != 0) {
        remainder = originalNum % 10;
        result += remainder * remainder * remainder;
        originalNum /= 10;
    }

    if (result == num) {
        return 1;
    } else {
        return 0;
    }
}

Let's consider an illustration to showcase the process of verifying an Armstrong number through a function in the C programming language.

Example

Example

#include <stdio.h>

#include <math.h>



// Function to check if a number is an Armstrong number

int isArmstrong(int num) {

    int originalNum = num, remainder, result = 0, n = 0;



    // Count number of digits

    int temp = num;

    while (temp != 0) {

        temp /= 10;

        n++;

    }



    temp = num;



    // Calculate the sum of digits raised to the power of n

    while (temp != 0) {

        remainder = temp % 10;

        result += pow(remainder, n);

        temp /= 10;

    }



    return (result == num);

}



int main() {   //main function

    int number;



    // Input from user

    printf("Enter a number: ");

    scanf("%d", &number);



    // Call the function

    if (isArmstrong(number))

        printf("%d is an Armstrong number.\n", number);

    else

        printf("%d is not an Armstrong number.\n", number);



    return 0;

}

</math.h></stdio.h>

Output:

Output

Enter a number: 9474

9474 is an Armstrong number.

Explanation:

In this instance, we are examining the isArmstrong function that executes various operations like digit counting, Armstrong sum calculation, and returning the result if the number qualifies as an Armstrong Number in the C programming language.

5) Armstrong Number Using Arrays

In the realm of C programming, this technique offers an organized approach for managing and storing separate digits of a numerical value. Within this technique, the number is initially broken down into its individual digits, with each digit being placed into an array. This strategy facilitates convenient retrieval of each digit for executing various operations, like exponentiation by the total digit count and aggregating the outcomes. Utilizing arrays enhances the clarity and adaptability of the logic, particularly when working with substantial numerical values.

C Example to check Armstrong Number using arrays

Let's consider a scenario to showcase the process of verifying an Armstrong number by utilizing arrays in the C programming language.

Example

Example

#include <stdio.h>

#include <math.h>



int main() {   //main function

    int num, temp, digits[10], count = 0, result = 0;



    // Input from user

    printf("Enter a Number: ");

    scanf("%d", &num);



    temp = num;



    // Store digits in array

    while (temp > 0) {

        digits[count] = temp % 10;

        temp /= 10;

        count++;

    }



    // Calculate the sum of each digit raised to the power of count

    for (int i = 0; i < count; i++) {

        result += pow(digits[i], count);

    }



    // Check and display result

    if (result == num)

        printf("%d is an Armstrong number.\n", num);

    else

        printf("%d is not an Armstrong number.\n", num);



    return 0;

}

Output:

Output

Enter a Number: 123

123 is not an Armstrong number.

Enter a Number: 153

153 is an Armstrong number.

Enter a Number: 9474

9474 is an Armstrong number.

Enter a Number: 54748

54748 is an Armstrong number.

Explanation:

In this instance, we've extracted the individual digits from the number and stored them in an array named digits. Subsequently, the count variable stores the total number of digits. Following this, each digit is exponentiated by the count and then summed up utilizing the pow function. Ultimately, the outcome is compared against the initial number.

Conclusion

In C development, the Armstrong number algorithm is a valuable resource for implementing modular C code, utilizing loops, arrays, recursion, and mathematical logic. Fundamental principles related to flow control, exponentiation, and digit manipulation can be demonstrated using arrays, recursive functions, or simple loop structures. In this context, various techniques are employed to verify whether a provided number qualifies as an Armstrong Number.

Armstrong Number in C FAQ'S

1) What is Armstrong's number for C programming?

In the realm of C programming, an Armstrong number refers to a unique figure where the sum of each digit raised to the total number of digits equals the original number itself. Engaging in this classic C activity allows for honing skills related to number manipulation, flow control, and mathematical computations.

To determine whether a number in the C programming language is an Armstrong number, we need to follow these steps:

Each numeral is isolated by utilizing the % and / operators, tallied, and subsequently exponentiated by that tally with the assistance of the pow function. The results are then summed, and compared against the original value. When these values are equivalent, the number qualifies as an Armstrong number.

3) What is the reason behind the usage of the pow function in Armstrong number programs?

Each numeral can be raised to the power of the total number of digits using the pow function from the math.h library. The task involves computing the Armstrong sum to verify its equivalence to the initial value.

4) Can Armstrong numbers be validated in C through recursive functions?

Yes, utilizing recursion is applicable for calculating both the Armstrong sum and digit counting. While limitations in stack depth might reduce its efficiency with larger numbers, it remains a more transparent and feasible method.

5) How does incorporating arrays or functions benefit Armstrong number programs?

Employing functions to enhance modularity and facilitate code reuse adds a layer of organization and clarity to programming. Arrays provide a means to store elements for easy index-based retrieval, enabling separate processing or display of each element.

Input Required

This code uses input(). Please provide values below: