Home

A program to find Armstrong numbers below given number

C Programming Language 028

The following code shows how to write a program to find Armstrong numbers below number 'n' and print them (NESTED WHILE).

Program

#include <stdio.h>

void main()
{
    int l, k = 1, q, r, sum = 0;

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

    printf("Armstrong numbers below %d are:\n", l);

    while (k <= l)
    {
        q = k;
        sum = 0;
        while (q > 0)
        {
            r = q % 10;
            sum = (r * r * r) + sum;
            q = q / 10;
        }

        if (sum == k)
        {
            printf("\t%d", sum);
        }
        k++;
    }

    printf("\n");
}

Result

Enter a number: 500
Armstrong numbers below 500 are:
        1       153     370     371     407


Last Updated on

Next Post: Rules vs Guidelines →

Comments