Finding Armstrong Number in C

CCBeginner
Practice Now

Introduction

In this lab, we will learn about Armstrong number and write a program in C to verify whether a number is an Armstrong number or not. An Armstrong number is a number, that is 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 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27.
371 is an Armstrong number, because 371 = 3^3 + 7^3 + 1^3 = 27 + 343 + 1.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/ControlFlowGroup -.-> c/while_loop("`While Loop`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-123200{{"`Finding Armstrong Number in C`"}} c/comments -.-> lab-123200{{"`Finding Armstrong Number in C`"}} c/variables -.-> lab-123200{{"`Finding Armstrong Number in C`"}} c/data_types -.-> lab-123200{{"`Finding Armstrong Number in C`"}} c/operators -.-> lab-123200{{"`Finding Armstrong Number in C`"}} c/if_else -.-> lab-123200{{"`Finding Armstrong Number in C`"}} c/while_loop -.-> lab-123200{{"`Finding Armstrong Number in C`"}} c/for_loop -.-> lab-123200{{"`Finding Armstrong Number in C`"}} c/user_input -.-> lab-123200{{"`Finding Armstrong Number in C`"}} c/memory_address -.-> lab-123200{{"`Finding Armstrong Number in C`"}} c/function_parameters -.-> lab-123200{{"`Finding Armstrong Number in C`"}} c/function_declaration -.-> lab-123200{{"`Finding Armstrong Number in C`"}} c/math_functions -.-> lab-123200{{"`Finding Armstrong Number in C`"}} end

Create a new C file

First, we need to create a new C file in the ~/project/ directory, let's name it main.c.

touch ~/project/main.c

Finding Armstrong numbers between 1 to 500

Let's write a C program to find Armstrong numbers between 1 to 500.

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

int main() {
    printf("\nArmstrong numbers between 1 to 500 are: \n");

    for (int i = 1; i <= 500; i++) {
        int t = i, sum = 0;
        while (t != 0) {
            int digit = t % 10;
            sum += pow(digit, 3);
            t /= 10;
        }
        if (sum == i) {
            printf("%d ", i);
        }
    }

    return 0;
}

Checking if a number is an Armstrong number

Now let's write a C program to check whether a number is an Armstrong number or not.

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

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

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

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

    return 0;
}

Putting it all together

Let's put both programs together in our main.c file.

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

int main() {
    // Armstrong numbers between 1 to 500
    printf("Armstrong numbers between 1 to 500 are: \n");

    for (int i = 1; i <= 500; i++) {
        int t = i, sum = 0;
        while (t != 0) {
            int digit = t % 10;
            sum += pow(digit, 3);
            t /= 10;
        }
        if (sum == i) {
            printf("%d ", i);
        }
    }

    printf("\n");

    // Checking if a number is Armstrong number
    int n, sum = 0;
    printf("Enter a number: ");
    scanf("%d", &n);

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

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

    return 0;
}

Summary

In this lab, we learned about Armstrong numbers and how to find them in a given range, and how to check whether a number is an Armstrong number or not. We hope you enjoyed this lab and it helped you learn more about C programming.

Other C Tutorials you may like