So far we have worked on obtaining individual digits from 4 digits of 5 digit numbers. The added them to find the sum of digits. However, now we know about the loop and we can remove the limit of having a specific number of digits. Write a program to print out all Armstrong numbers between 1 and n where n will be an user input. If the sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = (1 * 1* 1)+ ( 5 * 5* 5 ) + ( 3*3*3) In order to solve this problem we will implement the following function: sumDigitCube(): write a function sumDigitCube() that takes a positive integer as a parameter and returns the sum of cube of each digit of the number. Then in the main function, take an integer n as input and generate each number from 1 to n and call the sumDigitCube() function. Based on the returned result, compares it with the value of the generated number and take a decision and print the number if it is Armstrong number

Respuesta :

Answer:

#include<stdio.h>

int sumDigitCube(int n);

int main(){

   int n, i;

   printf("Enter number: ");

   scanf("%d", &n);

printf("The Armstrong numbers are:" );

   for(i=1; i<=n; i++){

       if(sumDigitCube(i)==i){

           printf(" %d", i);

       }

   }

   printf("\n");

   return 0;

}

int sumDigitCube(int n){

   int s = 0;

   int digit;

   while(n>0){

       digit = n%10;

       s += digit * digit *digit;

       n = n/10;

   }

   return s;

}

Explanation: