简体   繁体   中英

Armstrong number code in Java is not working right

I am completely new to Java and am writing code to check whether a number is an Armstrong number or not in the range 0 to 999.

Please tell me what is wrong. When run on command prompt, it repeatedly prints:

1 is the count

Code:

import java.util.*;
class Armstrong
{
    public static void main (String[] args)
    {
        int sum = 0;

        for (int i = 0; i < 1000; i++)
        {
            int n = i;
            int count =0;

            while(n > 0)
            {
                int mod = n % 10;
                n = n / 10;
                count++;
            }
            System.out.println(+count+ "is the count");

            for (int j = 1; j < count; j++)
            {   
                int val = i % 10;
                i= i / 10;
                sum = val * val * val + sum;
            }
            if (sum == i)
            {
                System.out.println( +i+ "is an Armstrong number");
            }       
        }   
    }
}

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.

For starters, you always raise the digits to the power of 3, so your calculation can only work for i between 100 and 999 .

Second, you are changing i within your inner loop, so the comparison at the end if (sum==i) will fail, since sum should be compared to the original i .

Next, you don't reset sum to 0 in each iteration of i .

You also skip one of the digits.

This seems to work :

    int sum = 0;

    for (int i = 100; i < 1000; i++) { // start at 100
        sum = 0; // clear the sum in each iteration
        int n = i;
        int count = 0;

        while (n > 0) {
            int mod = n % 10;
            n = n / 10;
            count++;
        }
        n = i;
        for (int j = 0; j < count; j++) { // iterate over all the digits

            int val = n % 10;
            n = n / 10; // don't change i
            sum = val * val * val + sum;

        }
        if (sum == i) {
            System.out.println(i + " is an Armstrong number");
        }
    }

This returns :

153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number

There is no need to run three loops. You can do it simpler that way:

for(int i = 0; i < 1000; i++) {
        int currNumber = i;
        int sum = 0;

        while(currNumber != 0)
        {
            int mod = currNumber % 10;
            sum = sum + mod * mod * mod;
            currNumber = currNumber / 10;
        }

        if (i == sum) {
            System.out.println(i + " is an Armstrong number");
        }
}

Print Armstrong number in java

public class CalculatArmstrong 
{   

public static void main(String[] args) 
{

    for(int i=0;i<=2000;i++)
    {

        int number=i;
        int n=number;
        int rem;
        int arms=0;

        while(number>0)
        {
            rem=number%10;

            arms=arms+rem*rem*rem;

            number=number/10;

        }


        if(n==arms)
        {

            System.out.println(n);
        }

    }

}

}

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