In this snippet we will write the program for Armstrong number.
What is Armstrong Number?
The sum of the cubes of individual digits of a number is equal to the same number.
Example1: Number 153 => 13 + 53 +33 = 1+125+27= 153
so 153 is Armstrong number.
Example1: Number 153 => 13 + 53 +33 = 1+125+27= 153
so 153 is Armstrong number.
Example2: Number 123 => 13 + 23 +33 = 1+8+27= 36. 36 is not equal to 123.
So 123 is not a Armstrong number.
Snippet:
class ArmStrongNumber
{
public
static void
Main()
{
Console.WriteLine("********** ArmStrong Number Check Example
********");
Console.WriteLine("Enter Number to Want to check");
int
numberToCheck = int.Parse(Console.ReadLine());
int
lenthOfNumber = numberToCheck.ToString().Length;
double
reminder = 0;
double
sum = 0;
int
tempNo = numberToCheck;
while
(tempNo > 0)
{
reminder = tempNo % 10;
sum = sum + Math.Pow(reminder, lenthOfNumber);
tempNo = tempNo / 10;
}
if
(sum == numberToCheck)
{
Console.WriteLine("The given Number {0} is a Armstrong Number",
numberToCheck);
}
else
{
Console.WriteLine("The given Number {0} is NOT a Armstrong
Number", numberToCheck);
}
Console.ReadLine();
}
}
|
Output:
Example : Input =153
Example : Input=123
|
0 Comments:
Post a Comment