In: Computer Science
An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
Write a C function to print all Armstrong numbers between a given interval. Then write a C program to keep reading two integers and print all Armstrong numbers between these two integers by calling that function.
Program code to copy:-
#include <stdio.h>
#include <math.h>
/* Function prototype */
void printArmstrong(int start, int end);
int main()
{
/* Declaration of variables two store two
integers*/
int num1, num2;
/*Prompt & read starting integer number of
interval*/
printf("Enter the starting number of interval:
");
scanf("%d", &num1);
/*Prompt & read ending integer number of
interval*/
printf("\nEnter the ending number of interval:
");
scanf("%d", &num2);
/*Calling function to print armstrong number in a
given interval
by passing two integer number*/
printArmstrong(num1, num2);
return 0;
}
/*Definition of function to print armstrong number in a given
interval.
This function receives two integer parameters which represents the
interval*/
void printArmstrong(int start, int end)
{
int i, n, sum, digit;
printf("\nThe Armstrong numbers between %d and %d:\n",
start, end);
/*The outer loop will exeuted within given inteval of
numbers*/
for(i=start; i<=end; i++)
{
/*Initilize sum with 0 for every
number to be checked for armstrong*/
sum = 0;
/*Making another copy of number to
be checked for armstrong*/
n = i;
/*Inner loop will calculate sum of
the cubes of digits of number*/
while(n!=0)
{
/*Extracting
last digit from number*/
digit = n %
10;
/*Adding sum of
the cube of digit*/
sum = sum +
pow(digit, 3);
/*Removing last
digit from number*/
n = n /
10;
}
/*Checking & printing the
armstrong number*/
if(i==sum)
printf("%d ",
i);
}
}
Screenshot of output:-