Write a c++ program that implements the functions with the following headers:
// Return the sum of the cubes of the digits in an integer,
// i.e., cubeOfDigits(131) returns 13 + 33 + 13 = 29
int cubeOfDigits(int number)
// Displays if integer is an Armstrong integer
void isArmstrong(int number)
Use cubeOfDigits to implement isArmstrong. An integer is an Armstrong integer(order of 3) if the sum of the cubes of its digits is equal to the number itself. Write aprogram that will print all the
Armstrong numbers within the range of 1 up to 1000.
Example program:
1
153
370
371
407
source code
#include <iostream>
#include <math.h>
using namespace std;
// Return the sum of the cubes of the digits in an integer,
int cubeOfDigits(int number){
int n;
int sum1=0;
while(number>0)
{
n=number%10;
sum1=sum1+pow(n,3);
number=number/10;
}
return sum1;
}
// Displays if integer is an Armstrong integer
void isArmstrong(int number){
cout<<"\nArmstrong numbers between 1 and "<<number<<" are: ";
for(int i=1;i<=number;i++){
if (i==cubeOfDigits(i))
cout<<"\n"<<i;
}
}
int main()
{
isArmstrong(500);
return 0;
}
Output
Comments
Leave a comment