Answer to Question #257519 in Java | JSP | JSF for Nikhil Sahoo

Question #257519

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.


1
Expert's answer
2021-11-01T18:47:41-0400
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);
		    }
		}
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS