Question

In: Computer Science

An Armstrong number is an integer such that the sum of the cubes of its digits...

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.

Solutions

Expert Solution

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:-


Related Solutions

An Armstrong number is a number that is the sum of its own digits, each raised...
An Armstrong number is a number that is the sum of its own digits, each raised to the power of the number of its digits. For example, 153 is considered an Armstrong number because 13 + 53 + 33 = 153. Write a VBA program that lists all 3-digit Armstrong numbers within a specified range of 3-digit positive integers. Your sub should do the following: 1) Using two input boxes, ask the user to input two 3-digit positive integers (a...
Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing...
Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing zeros (ie: the number of zeros at the end of the factorial). Input: integer n , a non-negative integer where n ≤ 100 Output: integer x y , the concatenation of x and y, where x is the sum of the digits in n! and y is the number of the zeros in n!) Note, 0 ≤ x , y . Example: Consider the...
4. Write a program to compute the sum of digits of an integer. For example, given...
4. Write a program to compute the sum of digits of an integer. For example, given the integer 35931, your program should output 21. (21 is the sum 3 + 5 + 9 + 3 + 1) PYTHON
Determine a two digit number whose value is equal to eight times the sum of its digits and when 45 is subracted from the number, the digits are reversed?
Determine a two digit number whose value is equal to eight times the sum of its digits and when 45 is subracted from the number, the digits are reversed?
In a two digit number the sum of the digits is 9. Also, when 27 is subtracted from the number the digits are reversed. Find the number?
In a two digit number the sum of the digits is 9. Also, when 27 is subtracted from the number the digits are reversed. Find the number?
//----------------------------------------------------------------- // Counts the number of odd, even, and zero digits in an integer // input...
//----------------------------------------------------------------- // Counts the number of odd, even, and zero digits in an integer // input value. Repeat as long as the user wishes to continue //----------------------------------------------------------------- public static void main(String[] args) {     // Declare the identifiers final int SENTINEL = -99;    // Declare the remaining identifiers ... Scanner scan = new Scanner(System.in);    // Display the programmer's information              // Input an integer number          // Count the number of odd, even, and...
A random number X is generated by reporting the sum of the digits on two randomly...
A random number X is generated by reporting the sum of the digits on two randomly chosen ping-pong balls, one from a bucket of two balls labeled “0” and “1” and one from a bucket of three balls labeled “0”, “1”, and “2”. Find the probability mass function of X by specifiying the values x that X can take and the probability p(x) = P (X = x) of each of those values. Please explain how you arrived at the...
Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm? use java
Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm?
SOLVE USING WHILE. A perfect number is a positive integer that is equal to the sum...
SOLVE USING WHILE. A perfect number is a positive integer that is equal to the sum of its positive divisors except the number itself. The first two perfect numbers are 6 and 28 since 1+2+3=6 and 1+2+ 4+7+14=28. Write a matlab computer program that finds the first n perfect number (the user must input the value of n) and show them in a vector thanks! xo
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT