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

Here an Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.

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 a, b, c, d;
        for (int i = 1; i <= 1000; i++)
        {
            a = i / 100;
            b = (i - a * 100) / 10;
            c = (i - a * 100 - b * 10);
            d = a * a * a + b * b * b + c * c * c;
            if (i == d)
            {
                System.Console.WriteLine("{0}", i);
            }
        } 
        Console.Read();
    }
}
Program Explanation

In this C# program, Using for loop compute the Armstrong numbers. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. Compute the division of the value of ‘i’ variable by 100.

advertisement
advertisement

Multiply the value of ‘a’ variable by 100 and compute the difference of the resulted value with the value of ‘i’ variable and divide the resulted value by 10. Compute the sum of the cube of ‘a’, ‘b’ and ‘c’ variables.

If condition statement is used to check the value of ‘i’ variable is equal to‘d’, if the condition is true then execute the statement and print the Armstrong numbers.

Runtime Test Cases
 
1
153
370
371
407
1000

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.