Check if a Number is an Armstrong Number in C

CCBeginner
Practice Now

Introduction

In this lab, we will learn how to check if a given number is an Armstrong number using a C program. We will first read an integer input from the user, then compute the sum of each digit raised to the power of the total number of digits, and finally print whether the number is an Armstrong number or not.

The program will guide the user through the process step-by-step, from reading the input to determining if the number is an Armstrong number.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435141{{"`Check if a Number is an Armstrong Number in C`"}} c/variables -.-> lab-435141{{"`Check if a Number is an Armstrong Number in C`"}} c/data_types -.-> lab-435141{{"`Check if a Number is an Armstrong Number in C`"}} c/operators -.-> lab-435141{{"`Check if a Number is an Armstrong Number in C`"}} c/user_input -.-> lab-435141{{"`Check if a Number is an Armstrong Number in C`"}} c/math_functions -.-> lab-435141{{"`Check if a Number is an Armstrong Number in C`"}} end

Read an Integer

In this step, we will learn how to read an integer input from the user in a C program to check if it is an Armstrong number.

First, let's create a new C file in the project directory:

cd ~/project
nano armstrong.c

Now, let's write the initial code to read an integer:

#include <stdio.h>

int main() {
    int number;

    // Prompt the user to enter a number
    printf("Enter a number to check if it's an Armstrong number: ");

    // Read the integer input
    scanf("%d", &number);

    // Print the entered number to verify input
    printf("You entered: %d\n", number);

    return 0;
}

Compile and run the program:

gcc armstrong.c -o armstrong
./armstrong

Example output:

Enter a number to check if it's an Armstrong number: 153
You entered: 153

Let's break down the code:

  • scanf("%d", &number) reads an integer from the user input
  • &number passes the memory address of the number variable
  • printf() is used to prompt the user and display the entered number

Compute Sum of Each Digit Raised to Power of Digits Count

In this step, we will modify our previous program to calculate the sum of each digit raised to the power of the total number of digits.

Open the armstrong.c file:

cd ~/project
nano armstrong.c

Update the code to compute the digit sum:

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

int main() {
    int number, originalNumber, remainder, digitCount = 0;
    long long digitSum = 0;

    // Prompt the user to enter a number
    printf("Enter a number to check if it's an Armstrong number: ");
    scanf("%d", &number);

    // Store the original number for later comparison
    originalNumber = number;

    // Count the number of digits
    while (number != 0) {
        number /= 10;
        digitCount++;
    }

    // Reset number to original value
    number = originalNumber;

    // Compute sum of each digit raised to power of digit count
    while (number != 0) {
        remainder = number % 10;
        digitSum += pow(remainder, digitCount);
        number /= 10;
    }

    // Print the results
    printf("Number of digits: %d\n", digitCount);
    printf("Sum of digits raised to power: %lld\n", digitSum);

    return 0;
}

Compile the program with math library:

gcc armstrong.c -o armstrong -lm
./armstrong

Example output:

Enter a number to check if it's an Armstrong number: 153
Number of digits: 3
Sum of digits raised to power: 153

Key concepts explained:

  • digitCount tracks the number of digits
  • pow(remainder, digitCount) raises each digit to the power of total digit count
  • number % 10 extracts the last digit
  • number /= 10 removes the last digit in each iteration
  • long long used to handle potentially large digit sums

Print If Armstrong or Not

In this step, we will complete our Armstrong number checker by comparing the original number with the computed digit sum.

Open the armstrong.c file:

cd ~/project
nano armstrong.c

Update the code to determine and print if the number is an Armstrong number:

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

int main() {
    int number, originalNumber, remainder, digitCount = 0;
    long long digitSum = 0;

    // Prompt the user to enter a number
    printf("Enter a number to check if it's an Armstrong number: ");
    scanf("%d", &number);

    // Store the original number for later comparison
    originalNumber = number;

    // Count the number of digits
    while (number != 0) {
        number /= 10;
        digitCount++;
    }

    // Reset number to original value
    number = originalNumber;

    // Compute sum of each digit raised to power of digit count
    while (number != 0) {
        remainder = number % 10;
        digitSum += pow(remainder, digitCount);
        number /= 10;
    }

    // Check if the number is an Armstrong number
    if (digitSum == originalNumber) {
        printf("%d is an Armstrong number.\n", originalNumber);
    } else {
        printf("%d is not an Armstrong number.\n", originalNumber);
    }

    return 0;
}

Compile the program:

gcc armstrong.c -o armstrong -lm
./armstrong

Example outputs:

Enter a number to check if it's an Armstrong number: 153
153 is an Armstrong number.

Enter a number to check if it's an Armstrong number: 154
154 is not an Armstrong number.

Key concepts explained:

  • Compare digitSum with the originalNumber
  • If they are equal, it's an Armstrong number
  • The program handles different input scenarios

Summary

In this lab, you will learn how to read an integer input from the user, compute the sum of each digit raised to the power of the total number of digits, and determine if the number is an Armstrong number. The first step involves prompting the user to enter a number and storing it in a variable. The second step focuses on calculating the sum of each digit raised to the power of the total number of digits, which is then compared to the original number to check if it is an Armstrong number.

Other C Tutorials you may like