- What is an Armstrong number
- How to find Armstrong numbers using while loop
- How to find Armstrong numbers using do-while loop
- Complete code example with step-by-step explanation
- Key differences between while and do-while loop
Introduction
In this tutorial, we will learn how to write a C program to print all Armstrong numbers from 1 to N using while and do-while loop.
An Armstrong number (also called a Narcissistic number or Pluperfect number) is a number that is equal to the sum of the cubes of its individual digits.
For example: 1, 153, 370, 371, and 407 are Armstrong numbers.
Example: 153 is an Armstrong number because:
1³ + 5³ + 3³ = 1 + 125 + 27 = 153
💡 Armstrong Number: A number that equals the sum of the cubes of its digits. For example, 153 = 1³ + 5³ + 3³.
C Program using while loop
#include <stdio.h>
/* Write C Program to find all Armstrong numbers from 1 to N using while loop */
int main()
{
int r = 0, limit, sum = 0, n = 1, n1;
/* Input upper limit from user */
printf("Enter upper limit to find Armstrong number: ");
scanf("%d", &limit);
printf("Armstrong numbers between 1 to %d are: \n", limit);
while (n <= limit)
{
sum = 0;
n1 = n;
/* Calculate sum of cubes of digits */
while (n1 != 0)
{
r = n1 % 10;
sum += r * r * r;
n1 = n1 / 10;
}
/* Check for Armstrong number */
if (n == sum)
{
printf("%d, ", n);
}
n++;
}
return 0;
}
C Program using do-while loop
#include <stdio.h>
/* Write C Program to find all Armstrong numbers from 1 to N using do-while loop */
int main()
{
int r = 0, limit, sum = 0, n = 1, n1;
/* Input upper limit from user */
printf("Enter upper limit to find Armstrong number: ");
scanf("%d", &limit);
printf("Armstrong numbers between 1 to %d are: \n", limit);
do
{
sum = 0;
n1 = n;
/* Calculate sum of cubes of digits */
do
{
r = n1 % 10;
sum += r * r * r;
n1 = n1 / 10;
} while (n1 != 0);
/* Check for Armstrong number */
if (n == sum)
{
printf("%d, ", n);
}
n++;
} while (n <= limit);
return 0;
}
Sample Output
Enter upper limit to find Armstrong number: 407 Armstrong numbers between 1 to 407 are: 1, 153, 370, 371, 407
Another Example:
Enter upper limit to find Armstrong number: 1000 Armstrong numbers between 1 to 1000 are: 1, 153, 370, 371, 407
Program Explanation
Let's break down the code step by step using the while loop version:
- Variable Declaration:
int r=0, limit, sum=0, n=1, n1;limit— stores the upper rangen— current number being checkedsum— stores the sum of cubes of digitsr— stores the remainder (individual digit)n1— temporary variable for processing
- Input from User:
printf("Enter upper limit to find Armstrong number: ");scanf("%d", &limit);
— The user enters a number (e.g., 407), which is stored inlimit. - Outer while Loop:
while(n <= limit)
— This loop runs from 1 to limit. For each number, we check if it is an Armstrong number. - Calculate Sum of Cubes of Digits:
—n1 = n;(copy the current number)
— The inner while loop extracts each digit, calculates its cube, and adds it tosum.
Example for 153:- Digit 3: r=3, sum=27, n1=15
- Digit 5: r=5, sum=152, n1=1
- Digit 1: r=1, sum=153, n1=0
- Check Armstrong Condition:
if(n == sum)
— If the original numbernequals thesumof cubes of its digits, it is an Armstrong number and gets printed. - Increment:
n++;
— Move to the next number and repeat untiln > limit. - Return:
return 0;— successful program execution.
📝 Note: The do-while loop version works similarly, except the loop body executes at least once before checking the condition. In this program, both versions produce the same output.
Step-by-Step Approach
| Step | Action | Explanation |
|---|---|---|
| 1 | Declare variables | Declare limit, n, sum, r, and n1 |
| 2 | Read limit | Get upper limit from user |
| 3 | Initialize n=1 | Start checking from 1 |
| 4 | Calculate digit cubes | Extract digits, calculate sum of cubes |
| 5 | Check condition | If n equals sum of cubes, print n |
| 6 | Increment n | Move to next number |
| 7 | Repeat | Continue until n exceeds limit |
Algorithm to Find Armstrong Numbers
- Start
- Read the value of N (limit) from user
- Initialize n = 1
- While n <= N:
- Set sum = 0
- Set n1 = n
- While n1 != 0:
- r = n1 % 10
- sum += r * r * r
- n1 = n1 / 10
- If n == sum, print n
- n++
- End
💻 Practice Exercise
Challenge 1: Write a program to find all 3-digit Armstrong numbers (153, 370, 371, 407) without using loops.
Challenge 2: Write a program to check if a given number is an Armstrong number or not.
Challenge 3: Write a program to print Armstrong numbers between two given numbers (start and end range).
🔍 Click to Show Solution for Challenge 1
#include <stdio.h>
int main() {
int num, r, sum;
printf("3-digit Armstrong numbers are:\n");
for(num = 100; num <= 999; num++) {
sum = 0;
int temp = num;
while(temp != 0) {
r = temp % 10;
sum += r * r * r;
temp = temp / 10;
}
if(num == sum)
printf("%d, ", num);
}
return 0;
}
🔍 Click to Show Solution for Challenge 2
#include <stdio.h>
int main() {
int num, r, sum = 0, temp;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
while(temp != 0) {
r = temp % 10;
sum += r * r * r;
temp = temp / 10;
}
if(num == sum)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is NOT an Armstrong number.\n", num);
return 0;
}
Frequently Asked Questions
1. What is an Armstrong number?
An Armstrong number is a number that is equal to the sum of the cubes of its individual digits. For example, 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153.
2. What is the difference between while and do-while loop?
The while loop checks the condition first, then executes the body. The do-while loop executes the body first, then checks the condition. This means do-while guarantees at least one execution.
3. What are the Armstrong numbers between 1 and 1000?
The Armstrong numbers between 1 and 1000 are: 1, 153, 370, 371, 407.
4. What is the time complexity of this algorithm?
The algorithm has a time complexity of O(N × d) where N is the limit and d is the number of digits in each number. For 3-digit numbers, it's O(N × 3).
5. Can I calculate Armstrong numbers without using loops?
Yes, you can manually check each number, but loops make the program efficient and scalable for any range.
💡 Tip: Always validate the input to handle negative numbers and ensure the limit is positive.