C Exercises: Find the Armstrong number for a given range of number
C For Loop: Exercise-30 with Solution
Write a C program to find the Armstrong number for a given range of number.
Sample Solution:
C Code:
/*
When the sum of the cube of the individual digits of a number
is equal to that number, the number is called Armstrong number.
For example 153. Sum of its divisor is 13 + 53 + 33 = 1 + 125 + 27 = 153
*/
#include <stdio.h> // Include the standard input/output header file.
int main() {
int num, r, sum, temp; // Declare variables for the input number, remainder, sum, and a temporary variable.
int stno, enno; // Declare variables for the starting and ending range.
// Prompt the user to input the starting range.
printf("Input starting number of range: ");
scanf("%d", &stno); // Read the starting range from the user.
// Prompt the user to input the ending range.
printf("Input ending number of range : ");
scanf("%d", &enno); // Read the ending range from the user.
// Print a message indicating that Armstrong numbers will be displayed.
printf("Armstrong numbers in the given range are: ");
// Start a loop to iterate through the numbers in the specified range.
for (num = stno; num <= enno; num++) {
temp = num; // Set 'temp' to the current number for manipulation.
sum = 0; // Initialize 'sum' to zero.
// Start a loop to extract digits from 'temp'.
while (temp != 0) {
r = temp % 10; // Get the last digit of 'temp'.
temp = temp / 10; // Remove the last digit from 'temp'.
sum = sum + (r * r * r); // Calculate the sum of cubes of each digit.
}
// If the sum of cubes of digits is equal to the original number, print the number as an Armstrong number.
if (sum == num)
printf("%d ", num);
}
// Print a newline to separate the output.
printf("\n");
return 0; // Return 0 to indicate successful execution.
}
Sample Output:
Input starting number of range: 1 Input ending number of range : 1000 Armstrong numbers in given range are: 1 153 370 371 407
Flowchart:

C Programming Code Editor:
Previous: Write a C program to check whether a given number is an armstrong number or not.
Next: Write a program in C to display the pattern like a diamond.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join