C Program To Find Armstrong Numbers Between Range using Function

In today’s video tutorial lets find all the Armstrong numbers or Narcissistic numbers between user entered range, using function / method.

An Armstrong number or Narcissistic number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.

Example 1:
If number = 370
It has 3 digits: 3, 7 and 0. So n = 3.
result = 33 + 73 + 03 = 27 + 343 + 0 = 370.
So the original number 370 is equal to the result. So it’s an Armstrong Number.

Example 2:
If number = 8208
It has 4 digits: 8, 2, 0 and 8. So n = 4.
result = 84 + 24 + 04 + 84 = ‭4096‬ + 16 + 0 + ‭4096‬ = 8208.
So the original number 8208 is equal to the result. So it’s an Armstrong Number.

Related Read:
Swap 2 Numbers Using a Temporary Variable: C
Function / Methods In C Programming Language
C Program to Check Armstrong Number
C Program to print Armstrong Numbers Between Two Integers

Video Tutorial: C Program To Find Armstrong Numbers Between Range using Function


[youtube https://www.youtube.com/watch?v=nsVcZxEvIc8]

YouTube Link: https://www.youtube.com/watch?v=nsVcZxEvIc8 [Watch the Video In Full Screen.]


To count number of digits in a number

    int n = 0, temp;

    temp = num;

    while(temp)
    {
        temp = temp / 10;
        n++;
    }

To Iterate through the digits in the number

        while(num)
        {
            rem = num % 10;
            sum = sum + pow(rem, n);
            num = num / 10;
        }

To know above code logic, please visit and watch the video tutorial present at C Program to Check Armstrong Number.

Full Source Code: C Program To Find Armstrong Numbers Between Range using Function

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

float armstrong(int);

int main()
{
    int count, start, end, temp;

    printf("Enter start and end values\n");
    scanf("%d%d", &start, &end);

    if(start > end)
    {
        temp = start;
        start= end;
        end  = temp;
    }

    printf("Armstrong numbers between %d and %d are\n", start, end);

    for(count = start; count <= end; count++)
    {
        if(count == armstrong(count))
        {
            printf("%d is an Armstrong number\n", count);
        }
    }

    return 0;
}

float armstrong(int num)
{
    int rem, n = 0, temp;
    float sum = 0.0;

    temp = num;

    while(temp)
    {
        temp = temp / 10;
        n++;
    }

    while(num)
    {
        rem = num % 10;
        sum = sum + pow(rem, n);
        num = num / 10;
    }

    return(sum);
}

Output 1:
Enter start and end values
1
999
Armstrong numbers between 1 and 999 are
1 is an Armstrong number
2 is an Armstrong number
3 is an Armstrong number
4 is an Armstrong number
5 is an Armstrong number
6 is an Armstrong number
7 is an Armstrong number
8 is an Armstrong number
9 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number

Output 2:
Enter start and end values
500
99999
Armstrong numbers between 500 and 99999 are
1634 is an Armstrong number
8208 is an Armstrong number
9474 is an Armstrong number
54748 is an Armstrong number
92727 is an Armstrong number
93084 is an Armstrong number

Output 3:
Enter start and end values
1
99999
Armstrong numbers between 1 and 99999 are
1 is an Armstrong number
2 is an Armstrong number
3 is an Armstrong number
4 is an Armstrong number
5 is an Armstrong number
6 is an Armstrong number
7 is an Armstrong number
8 is an Armstrong number
9 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number
1634 is an Armstrong number
8208 is an Armstrong number
9474 is an Armstrong number
54748 is an Armstrong number
92727 is an Armstrong number
93084 is an Armstrong number

Output 4:
Enter start and end values
1
500
Armstrong numbers between 1 and 500 are
1 is an Armstrong number
2 is an Armstrong number
3 is an Armstrong number
4 is an Armstrong number
5 is an Armstrong number
6 is an Armstrong number
7 is an Armstrong number
8 is an Armstrong number
9 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number

Logic To Find Armstrong Numbers Between Range using Function

We ask the user to enter start and end value i.e., the range. Using for loop we iterate through all the numbers between start and end. For each iteration of for loop, variable count holds the number which we need to check if its Armstrong number or not.

Value of count is passed to function armstrong. Function armstrong counts the number of digits present in the integer number(we store it inside variable n) and then multiplies all the individual digits of the number by n and adds them. The sum is returned back to the calling function.

If value present in count and the value returned by armstrong method are equal, then the value present in count is an Armstrong number and will be displayed on the console window.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program to print Armstrong Numbers Between Two Integers

An Armstrong number or Narcissistic number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.

Example 1:
If number = 153
It has 3 digits: 1, 5 and 3. So n = 3.
result = 13 + 53 + 33 = 1 + 125 + 27 = 153.
So the original number 153 is equal to the result. So it’s an Armstrong Number.

Example 2:
If number = 1634
It has 4 digits: 1, 6, 3 and 4. So n = 4.
result = 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634.
So the original number 1634 is equal to the result. So it’s an Armstrong Number.

Related Read:
C Program To Reverse a Number
Basic Arithmetic Operations In C
while loop in C programming
if else statement in C
Calculate Sum of Digits: C Program
C Program to Check Armstrong Number
C Program to print Armstrong Numbers between 1 and 500

C Program to print Armstrong Numbers Between Two User Entered Integers


[youtube https://www.youtube.com/watch?v=lDc4Lx5-Ltc]

YouTube Link: https://www.youtube.com/watch?v=lDc4Lx5-Ltc [Watch the Video In Full Screen.]


To count the digits in a number

        num = temp = count;
        n   = sum  = 0;

        while(temp)
        {
            temp = temp / 10;
            n++;
        }

To Iterate through the digits in the number

        while(num)
        {
            rem = num % 10;
            sum = sum + pow(rem, n);
            num = num / 10;
        }

To know above code logic, please visit and watch the video tutorial present at C Program to Check Armstrong Number.

Full Source Code: C Program to print Armstrong Numbers Between Two User Entered Integers

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

int main()
{
    int count, limit, rem, temp, num, n;
    float sum;

    printf("Enter 2 integer numbers\n");
    scanf("%d%d", &count, &limit);

    printf("\nArmstrong numbers between %d and %d\n", count, limit);

    while(count <= limit)
    {
        num = temp = count;
        n   = sum  = 0;

        while(temp)
        {
            temp = temp / 10;
            n++;
        }

        while(num)
        {
            rem = num % 10;
            sum = sum + pow(rem, n);
            num = num / 10;
        }

        if(count == sum)
        {
            printf("%d is Armstrong number\n", count);
        }

        count++;
    }

    return 0;
}

Output:
Enter 2 integer numbers
1
99999

Armstrong numbers between 1 and 99999
1 is Armstrong number
2 is Armstrong number
3 is Armstrong number
4 is Armstrong number
5 is Armstrong number
6 is Armstrong number
7 is Armstrong number
8 is Armstrong number
9 is Armstrong number
153 is Armstrong number
370 is Armstrong number
371 is Armstrong number
407 is Armstrong number
1634 is Armstrong number
8208 is Armstrong number
9474 is Armstrong number
54748 is Armstrong number
92727 is Armstrong number
93084 is Armstrong number

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert