Reputation: 15
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.
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.
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
}
}
Upvotes: 0
Views: 172
Reputation: 1
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");
}
}
Upvotes: 0
Reputation: 78
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
Upvotes: 0
Reputation: 44844
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
}
Upvotes: 1