Write a function to print all Armstrong numbers between a given interval in C programming. How to print all Armstrong numbers between a given range using functions in C programming. C function to print all Armstrong numbers from 1 to n.

Input lower limit: 1

Input upper limit: 1000

Output armstrong numbers: 1, 153, 370, 371, 407

Happy coding ;)

**Example:**Input lower limit: 1

Input upper limit: 1000

Output armstrong numbers: 1, 153, 370, 371, 407

### Required knowledge

Basic C programming, Function### Logic to print Armstrong numbers using function

We have already seen in our previous posts about what is Armstrong numbers, how to check whether a number is Armstrong number or not and how to print all Armstrong numbers in given interval using loops. Here we will use the above logic to put into a function. Basically printing Armstrong numbers in a given range includes basic three steps:- Find last digit of the given number.
- Cube the last digit just found and add it to some variable sum. Repeat the above two steps till the number becomes 0.
- Check if the sum equals to original number then the given number is Armstrong otherwise not.

### Program to print Armstrong numbers using function

/** * C program to print all armstrong numbers between a given range */ #include <stdio.h> /* Function declarations */ int isArmstrong(int num); void printArmstrong(int start, int end); int main() { int start, end; /* Reads lowe and upper limit to of armstrong numbers */ printf("Enter lower limit to print armstrong numbers: "); scanf("%d", &start); printf("Enter upper limit to print armstrong numbers: "); scanf("%d", &end); printf("All armstrong numbers between %d to %d are: \n", start, end); printArmstrong(start, end); return 0; } /** * Checks whether the given number is armstrong number or not. * Returns 1 if the number is armstrong otherwise 0. */ int isArmstrong(int num) { int temp, lastDigit, sum; temp = num; sum = 0; /* Finds sum of cube of digits */ while(temp != 0) { lastDigit = temp % 10; sum += lastDigit * lastDigit * lastDigit; temp /= 10; } /* Checks if sum of cube of digits equals * to original number. */ if(num == sum) return 1; else return 0; } /** * Prints all armstrong numbers between start and end. */ void printArmstrong(int start, int end) { /* Iterates from start to end and print the current number * if it is armstrong */ while(start <= end) { if(isArmstrong(start)) { printf("%d, ", start); } start++; } }

Output

Enter lower limit to print armstrong numbers: 1

Enter upper limit to print armstrong numbers: 1000

All armstrong numbers between 1 to 1000 are:

1, 153, 370, 371, 407,

Enter upper limit to print armstrong numbers: 1000

All armstrong numbers between 1 to 1000 are:

1, 153, 370, 371, 407,

Happy coding ;)

- Function and recursion programming exercises index.
- C program to check prime, strong or armstrong numbers using functions.
- C program to print all prime numbers between two intervals using functions.
- C program to print all strong numbers between two intervals using functions.
- C program to print all perfect numbers between two intervals using functions.
- C program to print all natural numbers from 1 to n using recursion.
- C program to find sum of all natural numbers from 1 to n using recursion.
- C program to find reverse of any number using recursion.
- C program to find palindrome of any number using recursion.
- C program to find nth fibonacci term using recursion.
- C program find GCD (HCF) of any two numbers using recursion.

< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)