• C Program to Check Armstrong Number of n digits

    Welcome to this blog post where we will learn about the Armstrong number and how to write a C program to check if a given number is an Armstrong number or not.

    Introduction

    Armstrong number is a special kind of number in mathematics. It is named after Michael F. Armstrong, who first introduced this concept. An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits.

    For example, let’s take the number 153:

    1^3 + 5^3 + 3^3 = 153

    Here, we have 3 digits in the number 153, and the sum of each digit raised to the power of 3 is equal to the number itself. Hence, 153 is an Armstrong number.

    In this blog post, we will write a C program to check whether a given number is an Armstrong number or not.


    Main Body

    To write a C program to check Armstrong numbers, we will break down the problem into smaller steps. Let’s discuss each step in detail.

    Step 1: Take input from the user

    The first step is to take input from the user. We will prompt the user to enter a positive integer and store it in a variable.

    #include<stdio.h>
    
    int main() {
        int num;
        
        printf("Enter a number: ");
        scanf("%d", &num);
        
        return 0;
    }

    Step 2: Count the number of digits

    Next, we need to count the number of digits in the given number. We can do this by dividing the number by 10 repeatedly until it becomes zero. Each time we divide the number by 10, we increment a counter variable.

    int count = 0;
    int temp = num;
    
    while(temp != 0) {
        temp = temp / 10;
        count++;
    }

    Step 3: Calculate the sum of digits raised to the power of the number of digits

    Now, we will calculate the sum of each digit raised to the power of the number of digits. We will use a temporary variable to store the original number and manipulate another variable to calculate the sum.

    int sum = 0;
    temp = num;
    
    while(temp != 0) {
        int digit = temp % 10;
        sum = sum + pow(digit, count);
        temp = temp / 10;
    }

    Step 4: Check if the sum is equal to the original number

    Finally, we will check if the sum of each digit raised to the power of the number of digits is equal to the original number. If they are equal, then the given number is an Armstrong number; otherwise, it is not.

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

    Step 5: Combining all the steps together

    Now, let’s combine all the steps together to form our complete C program to check Armstrong numbers.

    #include<stdio.h>
    #include<math.h>
    
    int main() {
        int num;
        
        printf("Enter a number: ");
        scanf("%d", &num);
        
        int count = 0;
        int temp = num;
        
        while(temp != 0) {
            temp = temp / 10;
            count++;
        }
        
        int sum = 0;
        temp = num;
        
        while(temp != 0) {
            int digit = temp % 10;
            sum = sum + pow(digit, count);
            temp = 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;
    }

    Conclusion

    In this blog post, we have learned about Armstrong numbers and how to write a C program to check if a given number is an Armstrong number or not. We have seen each step in detail, from taking input from the user to checking if the sum of digits raised to the power of the number of digits is equal to the original number.

    Now that you have understood the concept and the implementation, you can try running the program with various numbers and test your understanding. Experiment with different inputs and see if you can predict the output correctly.

    Writing a program to check Armstrong numbers is a great exercise to practice your programming skills in C. It involves concepts like loops, conditionals, and the calculation of powers. By understanding and implementing this program, you can enhance your problem-solving abilities and deepen your knowledge of programming.

    If you want to explore further, you can try modifying the program to check Armstrong numbers of a specific number of digits, or even try writing a program to find all Armstrong numbers within a given range.

    Keep learning and happy coding!