4 different C program to check if a number is Armstrong number or not

C program to check if a number is Armstrong number or not:

In this C programming tutorial, we will learn how to check if a number is Armstrong number or not. This program will take one number as input from the user and print one message that it is an Armstrong number or it is not.

What is an Armstrong number:

A number of n digits is called an Armstrong number if the sum of the digits of the number, each raised to n is equal to the number.

For example, 153 is an Armstrong number. Because,

  • It has 3 digits
  • Sum of 1^3 + 5^3 + 3^3 is 153. So, it is an Armstrong number.

Algorithm:

We will follow the below algorithm to check if a number is Armstrong or not:

  • Get the number as input from the user.
  • Count the number of digits of the number.
  • Find the sum of each digit of the number raised to the total number of digits.
  • If the sum is equal to the number, it is an Armstrong number. Else, it is not. Print a message to the user.

Let’s write down the program now.

Method 1: C program program to check if a number is Armstrong number or not:

Below is the complete program:

#include <stdio.h>

int main()
{
    int no, totalDigits = 0, copyNumber, lastDigit, sum = 0, product;

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

    copyNumber = no;

    while (copyNumber != 0)
    {
        copyNumber /= 10;
        totalDigits++;
    }

    copyNumber = no;

    while (copyNumber != 0)
    {
        lastDigit = copyNumber % 10;

        product = 1;
        for (int i = 0; i < totalDigits; i++)
        {
            product *= lastDigit;
        }

        sum += product;
        copyNumber /= 10;
    }

    if (sum == no)
    {
        printf("It is an Armstrong number");
    }
    else
    {
        printf("It is not an Armstrong number");
    }

    return 0;
}

Here,

  • We have initialized 6 different integer variables. no is to hold the user input number, totalDigits is to hold the total digits of the number, copyNumber will hold a copy of the number, lastDigit is to hold the last digit, sum is to hold the final sum and product is to hold the digit raised to total digits.
  • It asks the user to enter a number, reads it and stores it in the no variable.
  • The first while loop is calculating the total number of digits in the number. We are assigning the value of no to copyNumber to make a copy and we are working on that copy without changing the original number. It removes the last digit of the number on each iteration and increments the total count by 1.
  • The second while loop is finding the sum. It gets the last digit and by using the for loop, it finds the digit raised to total digits. product is storing that value for each digit and we are adding the value of product to sum. This loop also works on a copy of the original number.
  • At the end, we are comparing the sum with the original number and printing one message if the entered number is an Armstrong number or not.

It will print output as like below:

Enter a number: 154
It is not an Armstrong number

Enter a number: 153
It is an Armstrong number

Method 2: C program to check if a number is Armstrong number or not using different methods:

In the above example, everything is placed inside the main method. We can make some improvement to this program by creating separate methods.

Let’s write this program by breaking it into methods:

#include <stdio.h>

int getTotalDigits(int n)
{
    int totalDigits = 0;

    while (n != 0)
    {
        n /= 10;
        totalDigits++;
    }

    return totalDigits;
}

int getSumDigitsRaised(int n, int totalDigits)
{
    int lastDigit, sum = 0, product;
    while (n != 0)
    {
        lastDigit = n % 10;

        product = 1;
        for (int i = 0; i < totalDigits; i++)
        {
            product *= lastDigit;
        }

        sum += product;
        n /= 10;
    }

    return sum;
}

int main()
{
    int no;

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

    int totalDigits = getTotalDigits(no);
    int sum = getSumDigitsRaised(no, totalDigits);

    if (sum == no)
    {
        printf("It is an Armstrong number");
    }
    else
    {
        printf("It is not an Armstrong number");
    }

    return 0;
}

Here,

  • I created two new methods.

    • getTotalDigits, to get the total digits of a number. It takes the number as the parameter and returns the total digit.
    • getSumDigitsRaised, to get the sum of each digit raised to the total digits. It takes the number and the total number of digits as the parameters and returns the total sum of each digit raised to total digits.
    • These methods are called from main and the final sum is stored in the sum variable. It compares the sum with the original number to find if the entered number is an Armstrong number or not.

It will give similar output.

Method 3: Cleaning the main method:

We can also move everything out of the main method. We can create another method that will check if a number is Armstrong or not and return one value:

#include <stdio.h>

int getTotalDigits(int n)
{
    int totalDigits = 0;

    while (n != 0)
    {
        n /= 10;
        totalDigits++;
    }

    return totalDigits;
}

int getSumDigitsRaised(int n, int totalDigits)
{
    int lastDigit, sum = 0, product;
    while (n != 0)
    {
        lastDigit = n % 10;

        product = 1;
        for (int i = 0; i < totalDigits; i++)
        {
            product *= lastDigit;
        }

        sum += product;
        n /= 10;
    }

    return sum;
}

int isArmstrong(int n)
{
    int totalDigits = getTotalDigits(n);
    int sum = getSumDigitsRaised(n, totalDigits);

    return sum == n;
}

int main()
{
    int no;

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

    if (isArmstrong(no))
    {
        printf("It is an Armstrong number");
    }
    else
    {
        printf("It is not an Armstrong number");
    }

    return 0;
}
  • I created a new method called isArmStrong that takes one number as the parameter and returns 1 if it is an Armstrong number, else it returns 0.
  • This method is called from main

C armstrong number

Method 4: By using pow():

pow method is used to find the power of a number raised to another number. We can use this method to find the power.

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

int getTotalDigits(int n)
{
    int totalDigits = 0;

    while (n != 0)
    {
        n /= 10;
        totalDigits++;
    }

    return totalDigits;
}

int getSumDigitsRaised(int n, int totalDigits)
{
    int lastDigit, sum = 0, product;
    while (n != 0)
    {
        lastDigit = n % 10;

        sum += pow(lastDigit, totalDigits);
        n /= 10;
    }

    return sum;
}

int isArmstrong(int n)
{
    int totalDigits = getTotalDigits(n);
    int sum = getSumDigitsRaised(n, totalDigits);

    return sum == n;
}

int main()
{
    int no;

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

    if (isArmstrong(no))
    {
        printf("It is an Armstrong number");
    }
    else
    {
        printf("It is not an Armstrong number");
    }

    return 0;
}

In this program the for loop is replaced by pow.

It will give similar output.