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 \Large n^{th} power of its every digit is equal to the original number. The \Large n is the number of digits in the input number. This concept is explained in more detail in the problem section.

Problem Definition

What is an Armstrong Number?

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

  1. We take each digit of the number \Large N separately
  2. Compute \Large n^{th} power of each digit, and
  3. Take the sum, \Large S of all the power obtained in step 2.
  4. If the sum is equal to original number, \Large N
  5. Then the given number \Large N is an Armstrong number.

For Example

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

The number \Large N in above example is \Large 371 and number of digits \Large n = 3. So we raise each of the digits of \Large 371 to power to \Large 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 \Large N

Step 2 – Split each digit of the number

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

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

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

Step 6 – Print the Output.

Flowchart – Check Armstrong Number

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

Program Codes – 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;
}
#include <iostream>
using namespace std;

int main() {
    int n, n1, rem, num = 0;

    cout << "Enter a positive integer: ";
    cin >> n;

    n1 = n;
    while (n1 != 0) {
        rem = n1 % 10;
        num += rem * rem * rem;
        n1 /= 10;
    }

    if (num == n) {
        for (int i = 0; i < 30; i++) cout << "_";
        cout << "\n\n" << n << " is an Armstrong number\n";
        for (int i = 0; i < 30; i++) cout << "_";
        cout << "\n";
    } else {
        for (int i = 0; i < 30; i++) cout << "_";
        cout << "\n\n" << n << " is not an Armstrong number\n";
        for (int i = 0; i < 30; i++) cout << "_";
        cout << "\n";
    }

    return 0;
}
import java.util.Scanner;

class ArmstrongNumber {
    public static void main(String[] args) {
        int n, n1, rem, num = 0;

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        n = sc.nextInt();

        n1 = n;
        while (n1 != 0) {
            rem = n1 % 10;
            num += rem * rem * rem;
            n1 /= 10;
        }

        if (num == n) {
            for (int i = 0; i < 30; i++) System.out.print("_");
            System.out.println("\n\n" + n + " is an Armstrong number");
            for (int i = 0; i < 30; i++) System.out.print("_");
            System.out.println();
        } else {
            for (int i = 0; i < 30; i++) System.out.print("_");
            System.out.println("\n\n" + n + " is not an Armstrong number");
            for (int i = 0; i < 30; i++) System.out.print("_");
            System.out.println();
        }

        sc.close();
    }
}
n = int(input("Enter a positive integer: "))
n1 = n
num = 0

while n1 != 0:
    rem = n1 % 10
    num += rem * rem * rem
    n1 //= 10

if num == n:
    print("_" * 30)
    print(f"\n{n} is an Armstrong number")
    print("_" * 30)
else:
    print("_" * 30)
    print(f"\n{n} is not an Armstrong number")
    print("_" * 30)

Output

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

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