C Program To Check An Armstrong Number

This C program checks an input number whether it is an Armstrong number on not and prints the results. An Armstrong number is special number sum of the nth power of its every digit is equal to the original number. The n is the number of digits in the input number. This concept is explained in more detail in the problem section.

We compiled the program using Dev C++ version 4 compiler installed on Windows 7 64-bit. You can use a different compiler if you like after modifying the source code according to your compiler specifications. This is necessary to compile an error-free program.

Advertisements

You must be familiar with following C programming concept to understand this example.

Problem Definition

What is an Armstrong Number?

Suppose there is a number N, this number has n digits, if

  1. We take each digit of the number N separately
  2. Compute nth power of each digit, and
  3. Take the sum, S of all the power obtained in step 2.
  4. If the sum is equal to original number, N;
  5. Then the given number N is an Armstrong number.

For Example

\begin{aligned}
&371 = 3^3 + 7^3 + 1^3\\ \\
&= 27 + 343 + 1\\ \\
&=371
\end{aligned}

The number N in above example is 371 and number of digits n = 3. So we raise each of the digits of 371 to power to 3. Then the sum of all power is equal to the original number if the number is an Armstrong number.

How do we process the Input number N?

Step 1 – Get the number N

Advertisements

Step 2 – Split each digit of the number

Step 3 – Raise each digit to the power of 3 and take a sum of all the powers.

Step 4 – Repeat step 1 to 3 if number N not equal to 0.

Step 5 – Check if Sum == N after Step 4, N = = 0.

Step 6 – Print the Output.

Flowchart – Check Armstrong Number

Flowchart – C Program to check if a number is Armstrong number or not

Program Code – Armstrong Number

#include <stdio.h>
#include <conio.h>
main()
{
    int n,i,n1,rem,num =0;
    /* Read the input number */
    printf("Enter a positive integer:");
    scanf("%d",&n);
    /*Get the sum of nth power of each digit */
    n1 = n;
    while(n1 != 0)
    {
        rem = n1 % 10;
        num += rem * rem * rem;
        n1/=10;
    }
    /* Check if the number is an Armstrong or Not */
    if(num == n)
    {
    /*Print the result */
        for(i =0;i<30;i++)
        {
            printf("_");
        }
    printf("\n\n");
    printf(" %d is an Armstrong number\n",n);
        for(i =0;i<30;i++)
        {
            printf("_");
        }
    printf("\n\n");
    }
    else
    {
        for(i =0;i<30;i++)
        {
            printf("_");
            printf("\n\n");
        }
    printf("%d is not an Armstrong number",n);
        for(i =0;i<30;i++)
        {
            printf("_");
            printf("\n\n");
        }
    }
    getch();
    return 0;
}

Output

Here is the output of the program for an input integer 371.

 Enter a positive integer:371
 ____________________________
 371 is an Armstrong number
 ____________________________
Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version