0

I wrote a code for school in java that verifies if a number is an armstrong number. I programmed it so that it runs as many times until the user inputs 0, at which the program will terminate. I am having 2 problems.

  1. The code only works the first time through, if the user inputs 371 (an armstrong number) the first time, it works, but after that it returns that the number is not an armstrong number.

  2. When the user inputs 0, it still shows the statement whether it is or is not an armstrong number, which I don't want it to.

This is the code:

import java.util.Scanner; //import Scanner for user input   

public class Ch6Project {    

    public static void main(String[] args) {  

        int userNum, totalValue = 0, num, numLength; //declare variables that will be used
        String suserNum; //declare user input variable
        Scanner input = new Scanner(System.in); //declare a Scanner

        System.out.println("Welcome to the Armstrong Number Program."); //description
        System.out.println("\nTo calculate an Armstrong number: ");
        System.out.println("\t 1. Cube each digit of the number.");
        System.out.println("\t 2. Take the sum of these cubes.");
        System.out.println("\t 3. If the sum equals the number, it is an Armstrong Number.");
        System.out.println("\t e.g. 3^3 + 1^3 + 7^3 = 317");

        do {
            System.out.print("\nEnter a whole number (0 to quit): ");
            suserNum = input.nextLine(); //collect user input
            userNum = Integer.parseInt(suserNum); //parse user input
            numLength = suserNum.length(); //calculate length of user input

            for (int i = numLength; i > 0; i--) { //create loop to run for n times 
                num = Integer.parseInt(suserNum.substring(numLength - 1, numLength)); //get last digit of number
                totalValue += Math.pow(num, 3); //cube a digit 
                numLength--; //subtract userNum by 1 to get the rest of the digits
            }

            if (totalValue == userNum) { //if total value equals user input, it is Armstrong #
                System.out.println("Your number is an Armstrong number.");
            } else { //if total value does not equal user input, it is not an Armstrong #
                System.out.println("Your number is not an Armstrong number.");
            }

        } while (userNum != 0); //run loop until user input == 0
        input.close(); //close user input

    }
}
Mixone
  • 1,327
  • 1
  • 13
  • 24

3 Answers3

1

Change your code so that it breaks immediately after entry of the userNum

e.g.

userNum = Integer.parseInt(suserNum); //parse user input
if (userNum == 0) {
   break;
}

then you can also change your loop to a endless loop

while (true) {
   // your code
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

It works only the first time because you don't reset your sum variable: just after the do insert:

do { totalValue =0;.. }while (userNum != 0);

plus, your code doesn't immediatly exit because the check of the condition is at the end, hence you insert the number, the code is executed and THEN you check. BTW: when you can, avoid using break statement.

Put an if controll after the reading of the number, like this:

do {
    System.out.print("\nEnter a whole number (0 to quit): ");
    suserNum = input.nextLine(); //collect user inputuserNum = 
    Integer.parseInt(suserNum); //parse user input

    if(userNum !=0){...your code...}
}while (userNum != 0);

There are a ton of more elegant way to code it but this should do the work

  • 1
    *avoid using break statement.* - I have never heard this before. Do you have something to back this up? I found the opposite https://stackoverflow.com/questions/18188123/is-it-bad-practice-to-use-break-to-exit-a-loop-in-java – Scary Wombat Jan 15 '18 at 02:58
  • Yes, sorry, my bad, I didn't specified. It's a mater of style actually. I came form C where break statements "break" structured programming. Making some search you're right it doesn't affect the execution of the code (but still i don't like it) – Silvio Zanoli Jan 15 '18 at 12:01
  • 1
    No problem, I agree that a loop with multiple break statements can be a bit hard to follow, and `break with label` can be even worse. – Scary Wombat Jan 15 '18 at 23:51
  • Switch case all the way – Mixone Jan 19 '18 at 09:24
0
public class E1 {
   public static void main(String[] args) {
      int c = 0, a, temp;
      int m = 153;
      int n = m;
      temp = n;
      while (n > 0) {
         a = n % 10;
         n = n / 10;
         c = c + (a * a * a);
      }
      if (temp == c)
         System.out.println(m + " is an armstrong number");
      else
         System.out.println(m + "is not an armstrong number");
   }
}
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 01 '22 at 12:36