C# Program to Print Armstrong Number between 1 to 1000

Exploring the ‘C# Program to Print Armstrong Number between 1 to 1000’ is a fascinating dive into the world of coding that blends both logic and creativity. Curious minds often wonder about the elegance behind these numbers and how such a program comes together. In this blog, we’ll unravel the magic behind Armstrong numbers and discover how to implement this intriguing concept using C#. Stick around, and you might just unlock a new level of coding prowess!

What is an Armstrong Number?

An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. A number is called an Armstrong number if:
Sum of (each digit raised to the power of number of digits) = the number itself

Example:

153 is an Armstrong number because:

  • It has 3 digits: 1, 5, and 3
  • Calculate:
    13+53+33=1+125+27=1531^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

Since the result equals the original number, 153 is an Armstrong number.

Armstrong Number Code

C# Program to Print Armstrong Numbers Between 1 to 1000

using System;

class ArmstrongNumbers
{
    static void Main()
    {
        Console.WriteLine("Armstrong numbers between 1 and 1000 are:");

        // Looping through numbers from 1 to 1000
        for (int num = 1; num <= 1000; num++)
        {
            int sum = 0, temp = num;
            int digits = num.ToString().Length;

            // Extracting digits and calculating the sum of powers
            while (temp > 0)
            {
                int digit = temp % 10;
                sum += (int)Math.Pow(digit, digits);
                temp /= 10;
            }

            // Check if the number is an Armstrong number
            if (sum == num)
            {
                Console.WriteLine(num);
            }
        }
    }
}

Explanation of the Logic Used in the Program

The program checks each number between 1 and 1000. For each number:

  1. It calculates how many digits the number has.
  2. Then, it extracts each digit and raises it to the power equal to the number of digits.
  3. It adds up these powered digits.
  4. If the sum equals the original number, it’s an Armstrong number.

Step-by-Step Breakdown

Looping through 1 to 1000

We use a for loop to check every number from 1 to 1000.

for (int num = 1; num <= 1000; num++)

Extracting Digits

We extract digits one by one using % 10 and reduce the number using / 10.

int digit = temp % 10;
temp /= 10;

Calculating the Cube (or nth power) of Each Digit

We raise each digit to the power of the number of digits:

sum += (int)Math.Pow(digit, digits);

Checking the Armstrong Condition

If the sum of powered digits equals the original number, it’s printed.

if (sum == num)
    Console.WriteLine(num);

Output of the Program

Armstrong numbers between 1 and 1000 are:
1
2
3
4
5
6
7
8
9
153
370
371
407

These are all the Armstrong numbers between 1 and 1000.

Real-Life Applications of Armstrong Numbers in C#


  1. Gaming Companies and Random Number Generation: Often, gaming platforms need to test algorithms involving unique number sequences. By understanding Armstrong numbers, companies can generate patterns needed for puzzles or in-game challenges, leading to new and interesting gameplay mechanics. Some developers incorporate these concepts into their games to increase complexity, encouraging player engagement and retention.

  2. Corporate Programming Training: Corporations sometimes use challenges like finding Armstrong numbers within specified ranges to train new developers. This exercise helps trainees understand loop iteration and condition checking, which are vital in real-life coding scenarios where precise logic is necessary, such as validating inputs or filtering data.

  3. Educational Software Development: Educators developing math-intensive software integrate Armstrong numbers to test learners’ comprehension of mathematics and number theory. It provides a practical application by designing exercises where students can actively calculate and verify these numbers, blending programming education with math learning.

  4. Data Security Systems: Some security systems leverage concepts similar to Armstrong numbers to generate unique keys or codes. The mathematical uniqueness serves as an effective tool for creating secure digital locks or encoding messages, ensuring data protection and integrity while maintaining computational simplicity and reliability.

Quiz on Armstrong Numbers


  1. What is an Armstrong number?

    • A number that is the sum of its digits’ cubes

    • A number that equals ten times itsfirst digit

    • A relatively prime number



  2. How many Armstrong numbers exist between 1 to 1000?

    • 4

    • 5

    • 6



  3. Which of the following is an Armstrong number?

    • 123

    • 370

    • 456



  4. Which C# keyword is used to iterate through the numbers from 1 to 1000?

    • for

    • if

    • switch



  5. What operation is critical when checking for an Armstrong number?

    • Subtraction

    • Addition

    • Power

Imagine effortlessly writing and testing code with our AI-powered csharp online compiler. Instantly run and debug programs, all driven by intelligent AI support. Dive into coding without barriers. It’s like having a coding partner guiding you step by step, simplifying complex tasks. Start your coding journey today!

Common Mistakes to Avoid

1. Misunderstanding the Cube Logic

Many learners mistakenly cube every digit regardless of the number’s length. Remember:

  • You must raise each digit to the power equal to the number of digits.
  • For example, 9474 has 4 digits, so each digit must be raised to the 4th power, not 3.

2. Errors in Digit Extraction

Incorrectly using division or forgetting to update the number in the loop can lead to wrong results.

  • Make sure to correctly extract digits using temp % 10 and reduce the number using temp /= 10.
  • Always reset your temporary variable (temp = num) before processing.

Practice Exercise

Want to test your understanding? Try this:

Modify the Program

Change the range from 1 to 1000 to 1 to 10,000 and see which new Armstrong numbers appear.

for (int num = 1; num <= 10000; num++)

Create a Reusable Function

Encapsulate the Armstrong check into a method. This will improve reusability and clarity.

static bool IsArmstrong(int number)
{
    int sum = 0, temp = number;
    int digits = number.ToString().Length;

    while (temp > 0)
    {
        int digit = temp % 10;
        sum += (int)Math.Pow(digit, digits);
        temp /= 10;
    }

    return sum == number;
}

Then call it like this:

for (int num = 1; num <= 10000; num++)
{
    if (IsArmstrong(num))
        Console.WriteLine(num);
}

This not only makes your code cleaner but also helps you follow best programming practices.

Conclusion

C# Program to Print Armstrong Number between 1 to 1000 is a great way to boost your programming skills. Not only do you learn vital C# concepts, but you also gain problem-solving abilities that are super useful in real-world coding. Why not give it a go? To dive deeper into programming languages like Java, Python, C, and C++, visit Newtum for more helpful resources.

Edited and Compiled by

This article was compiled and edited by @rasikadeshpande, who has over 4 years of experience in writing. She’s passionate about helping beginners understand technical topics in a more interactive way.

About The Author