Author : Rahul

Write a program to check whether entered number is armstrong or not


Armstrong Number

A number is said to be Armstrong if the sum of the cube of its digits is the same as the number itself. For Example, 153 is an Armstrong Number because the sum of the cube of its digit is 1*1*1 + 3*3*3 + 5*5*5 = 153 which is the same as the original number 153.

Program

using System;

namespace ProgrammingQuestion
{
 class Program
 {
   static void Main(string[] args)
   {
     //Variable Declaration
     string checkVal, message;
     int checkNum, digit, newNum = 0, tempNum = 0;
     bool isArmstrong;
     
     //User Input
     Console.Write("Enter Number:");
     checkVal = Console.ReadLine();
     checkNum = Convert.ToInt32(checkVal);
     
     //Armstrong Logic
     tempNum = checkNum;
     while(tempNum > 0)
     {
       digit = tempNum % 10;
       newNum = newNum + (int)Math.Pow(digit, 3);
       tempNum = tempNum / 10;
     }
     isArmstrong = checkNum == newNum;
     
     //Print Result
     if(isArmstrong)
     {
       message = $"Entered Number {checkNum} is an armstrong number.";
     }
     else
     {
       message = $"Entered Number {checkNum} is not an armstrong number.";
     }
     Console.WriteLine(message);
     Console.Read();
   }
 }
}

Output

Enter Number:153
Entered Number 153 is an armstrong number.