WAP to take two numbers as input then display all Armstrong number within them in descending order in one line. Also display how many such numbers found using following method prototype: int armstrong(int a,int b)
where a=99 and b<=1000.
class ArmStrong{
int power_number(int first, long second)
{
if( second == 0)
return 1;
if (second%2 == 0)
return power_number(first, second/2)*power_number(first, second/2);
return first*power_number(first, second/2)*power_number(first, second/2);
}
int ordering(int number)
{
int k = 0;
while ( number!= 0)
{
k++;
number = number/10;
}
return k;
}
boolean isArmstrong (int number)
{
int n = ordering(number);
int temp=number, total=0;
while (temp!=0)
{
int r = temp%10;
total = total + power_number(r,n);
temp = temp/10;
}
return (total == number);
}
int armstrong(int a,int b){
int count = 0;
for(int i=b; i>=a; i++){
if(isArmstrong(i)){
count++;
}
}
return count;
}
}
public class Main
{
public static void main(String[] args) {
ArmStrong a = new ArmStrong();
for(int i=200; i>=0; i--){
if(a.isArmstrong(i)){
System.out.println(i);
}
}
}
}
Comments
Leave a comment