C# Program to Print Armstrong Number between 1 to 1000

This is a C# Program to print all the armstrong numbers from 1 to 1000.

Problem Description

This C# Program Prints all the Armstrong Numbers from 1 to 1000.

Problem Solution

An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For the given problem, we will have to check for 1-digit number, 2-digit number, 3-digit number and 4-digit number (1000).

Program/Source Code

Here is source code of the C# Program to Print all the Armstrong Numbers from 1 to 1000. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Print all the Armstrong Numbers from 1 to 1000
 */
 
using System;
class Program
{
  static void Main ()
  {
    int num, a, b, c, d;
    for (int i = 1; i <= 1000; i++)
    {
    num = i;
    a = num / 1000;   // thousands place
    num = num % 1000;
    b = num / 100;    // hundreds place
    num = num % 100;
    c = num / 10;     // tens place
    d = num % 10;     // units place
 
    // Check if the sum of the cubes of the digits equals the original number
    if (i == a*a*a*a + b*b*b*b + c*c*c*c + d*d*d*d)
      {
        System.Console.WriteLine(i);
      }
    else if (i == b*b*b + c*c*c + d*d*d)  // for 3-digit numbers
      {
        System.Console.WriteLine(i);
      }
    else if (i == c*c + d*d)  // for 2-digit numbers
      {
        System.Console.WriteLine(i);
      }
    else if (i == d)  // for single-digit numbers
      {
        System.Console.WriteLine(i);
      }
    }
    Console.Read();
  }
}
Program Explanation

In this C# program, Using for loop compute the Armstrong numbers. The code extracts each digit of the number by dividing and using the modulus operator.
– a is the thousands place.
– b is the hundreds place.
– c is the tens place.
– d is the units place.

advertisement
advertisement

If the number matches the condition for being an Armstrong number, it is printed.

Runtime Test Cases
1
2
3
4
5
6
7
8
9
153
370
371
407

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.