Print all Armstrong numbers between given intervals using functions.
#include <stdio.h>
/* Function declarations */
int isArmstrong(int num);
void printArmstrong(int first, int last);
int main()
{
int first, last;
/* Input lower and upper limit to of armstrong numbers */
printf("Enter lower limit to print armstrong numbers: ");
scanf("%d", &first);
printf("Enter upper limit to print armstrong numbers: ");
scanf("%d", &last);
printf("All armstrong numbers between %d to %d are: \n", first, last);
printArmstrong(first, last);
return 0;
}
/**
* Check 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;
/* Calculate sum of cube of digits */
while (temp != 0)
{
lastDigit = temp % 10;
sum += lastDigit * lastDigit * lastDigit;
temp /= 10;
}
if (num == sum)
return 1;
else
return 0;
}
/**
* Print all armstrong numbers between first and last.
*/
void printArmstrong(int first, int last)
{
while (first <= last)
{
if (isArmstrong(first))
{
printf("%d ", first);
}
first++;
}
}
Output
Enter lower limit to print armstrong numbers : 1
Enter upper limit to print armstrong numbers : 200
All armstrong numbers between 1 to 200 are : 1 153
0 Comments