In this blog we will create a program in c# for check entered number is Armstrong number or not. 

What is Armstrong Number?

An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.A number that is the sum of its own digits each raised to the power of the number of digits.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.

Let's try to understand why 371 is an Armstrong number.

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


C# Code




 Code Explanation:

  • First of all we define four integer variable number , sum , rem and temp then assign zero (0) to sum .
  • Then get user input by Console.ReadLine() and pass this input to number variable by converting it to integer.
  • Now assign this user input number to our temp variable for future use.
  • Then we iterate while look till number became zero(0).
  • In loop
    • first we find Remainder of number by module its by 10.
    • Then we get power of this remainder by simply multiply it three times and add in our sum variable.
    • Then assign result of sum and power of remainder to sum variable.
    • In last divide number by 10.
  • After loop check temp variable and sum variable is equal. If both are equal then its Armstrong number otherwise not. 
  • Then We use the Console.ReadKey() method to read any key from the console. By entering this line of code, the program will wait and not exit immediately. The program will wait for the user to enter any key before finally exiting. If you don't include this statement in code, the program will exit as soon as it is run.

 

Output

Check Entered Number Is Armstrong Or Not In C#

Check Entered Number Is Armstrong Or Not In C#


I hope you like this blog and find some help. If you find it useful kindly share with friends.