C# Program to Print Armstrong Number between 1 to 1000
Posted by Superadmin on August 11 2022 07:55:03

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.

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