简体   繁体   中英

To Find if a Number is Armstrong in java(BlueJ)

A class HiArm has been defined to find whether a given number is an Armstrong number or not.

Armstrong number is a number that is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

Member functions:

  • HiArm() - constructor to assign 0 to n
  • HiArm(int a) - constructor to assign a to n
  • int sumArm() - to find and return the sum of the cube of the digits of the number
  • void isArm() - to invoke sumArm() and print if the number is Armstrong

write the main() method to input a number and call the above functions

Ps:- My approch:-

import java.util.*;
class HiArm{
    int n;
    int d;
    int b;
    int c;
    int g=n;
    HiArm(){
        n=0;
    }

    HiArm(int a){
        n=a;
    }

    int sumArm(){
        d = (int)(Math.log10(n) + 1);
        c=0;
        b=0;
        for(int i=1;i<=d;i++){
            b=g%10;
            b=b*b*b;
            c=c+b;  
            g=(g-(g%10))/10;
        }
        return c;
    }

    void isArm(){
        sumArm();
        if(c==n){
            System.out.println("The number is Armstrong");
        }
        else{
            System.out.println("The number is not Armstrong");
        }
    }

    public static void main(String args[]){
        Scanner sc =new Scanner(System.in);
        System.out.println("Enter a number to check whether it is an Armstrong or not");
        int k=sc.nextInt();
        HiArm o = new HiArm(k);
        o.isArm();        
    }
}

The function sumArm() always returns 0. Where did I go wrong?

I've made some changes on your code, let me explain ;) import java.util.*; class HiArm{

int value;
int sum;

HiArm(){
    value=0;
}

HiArm(int a){
    value=a;
}

int sumArm(){
    sum=0;
    for(char digit : String.valueOf(value).toCharArray()){
        sum += Math.pow(Integer.valueOf(digit)-48, String.valueOf(value).toCharArray().length);
    }
    return sum;
}

boolean isArm(){
    sumArm();
    /*
    if(sum==value){
        System.out.println("The number is Armstrong");
    }
    else{
        System.out.println("The number is not Armstrong");
    }
    */
    return sum==value;
}

public static void main(String args[]){
    Scanner sc =new Scanner(System.in);
    System.out.println("Enter a number to check whether it is an Armstrong or not");
    int k=sc.nextInt();
    HiArm o = new HiArm(k);
    System.out.println(o.isArm());        
}

}

So first your error : initialization or your g was made BEFORE the constructor so its value was null Then I allow me to change the names of parameters, you better do like this everytime to quickly know which parameter is doing what Then I made a loop on each digit, I transform the value into a String, then in a array of char and I iterat over it, for each one I calculate digit^3 and add it to the sum (the "-48" is because of their ASCII code"),

The Reason why it failed was cause g is assigned to n even before the constructor is called and is always 0 .

reassigning the variable g to n within the sumArm() function would fix the problem.

int sumArm(){
    d = (int)(Math.log10(n) + 1);
    c=0;
    b=0;
    g=n
    for(int i=1;i<=d;i++){
        b=g%10;
        b=b*b*b;
        c=c+b;  
        g=(g-(g%10))/10;
    }
    return c;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM