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

Question #258297

Write a program in Java to display the armstrong numbers formed by the combination of the factors of the first ten natural numbers using the method prototype: String num(int a)


1
Expert's answer
2021-10-28T15:20:57-0400


package amstrong;




public class Amstrong {


   public static boolean isArmstrongNumber(int num) {
        int total = 0, right, t;
        t = num;
        while (t != 0) {
            right = t % 10;
            total = total + (right * right * right);
            t = t / 10;
        }
       
        if (total == num) {
           
            return true;
        } else {
            
            return false;
        }
   }
   public static String num(int a){
       String factors = " ";
       for(int i=1; i<=a; i++){
           String se = " ";
           for(int j=1; j<i; j++){
               if(i%j==1){
                   se += String.valueOf(j);
               }
           }
           int k = Integer.parseInt(se);
           if(isArmstrongNumber(k)){
               factors += String.valueOf(k) +" ";
           }
       }
       return factors;
   }
    public static void main(String[] args) {
        System.out.println(num(10));
    }
    
}

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