An Armstrong number is the number whose sum of the cubes of the digits equal to the same number.
//function for checking the Armstrong number
public void ArmstrongCheck(int num)
{
//set a variable for storing sum
int sum = 0;
//variable for getting digit by digit of the number
int val = 0;
//Store the variable to another
int ArmNum = num;
//Continue till the number is zero
while (num != 0)
{
//get digit by digit using modulo
val = num % 10;
//get the next value
num = num / 10;
//sum of the cubes of digits
sum += val * val * val;
}
//sum equal to same number, then an Armstrong number
if (sum == ArmNum)
{
Console.WriteLine("Armstrong Number");
}
else
{
Console.WriteLine("Not an Armstrong Number");
}
//Input: 153
Output: Armstrong Number
//Input: 127
Output: Not an Armstrong Number
good for interviews. thankyou
ReplyDelete