Armstrong Number Program in C#

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. For example, 153 is an Armstrong number because 13+53+33=1531^3 + 5^3 + 3^3 = 153

Here's a simple C# program to check if a number is an Armstrong number:

 using System;  
 class Program  
 {  
   static void Main()  
   {  
     Console.WriteLine("Enter a number:");  
     int number = Convert.ToInt32(Console.ReadLine());  
     if (IsArmstrong(number))  
     {  
       Console.WriteLine($"{number} is an Armstrong number.");  
     }  
     else  
     {  
       Console.WriteLine($"{number} is not an Armstrong number.");  
     }  
   }  
   static bool IsArmstrong(int number)  
   {  
     int originalNumber = number;  
     int sum = 0;  
     int digits = number.ToString().Length;  
     while (number > 0)  
     {  
       int digit = number % 10;  
       sum += (int)Math.Pow(digit, digits);  
       number /= 10;  
     }  
     return sum == originalNumber;  
   }  
 }  

How It Works:

  1. The user is prompted to enter a number.
  2. The IsArmstrong method calculates the number of digits in the number and then computes the sum of each digit raised to the power of the total number of digits.
  3. Finally, it compares the calculated sum to the original number to determine if it's an Armstrong number.

Running the Program:

  1. Copy the code into a C# development environment or online compiler.
  2. Compile and run the program.
  3. Enter a number when prompted to check if it is an Armstrong number.

Thanks

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال