Write a C# program to check armstrong Number

How to write a c# program to check a number is Armstrong number or not. In the below example We will see what is an Armstrong number and how to find the Armstrong number in C#.

What is Armstrong Number

An Armstrong number is a sum of the cube of each number is equal to the same number.

Example:

371 = (3*3*3)+(7*7*7)+(1*1*1)
where:
(3*3*3)=27
(7*7*7)=343
(1*1*1)=1

So:
27+343+1=371

Check Armstrong number in C#

using System;
 
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            //Define variables
            int n, r, sum = 0, temp;
            
            //Print message to enter the number
            Console.Write("Enter the Number= ");
 
            //Read a number from console and convert it in the int
            n = int.Parse(Console.ReadLine());
            temp = n;
            //Execute loop
            while (n > 0)
            {
                r = n % 10;
                sum = sum + (r * r * r);
                n = n / 10;
            }
            if (temp == sum)
                Console.Write(n+ " is an armstrong Number");
            else
                Console.Write(n+ " is not an Armstrong Number.");
            Console.ReadLine();
        }
        
    }
}
 

 

Add a Comment

Your email address will not be published. Required fields are marked *

fifteen + 9 =