Question

Write a JAVA program to check whether a given number is an Armstrong number. An Armstrong...

Write a JAVA program to check whether a given number is an Armstrong number. An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// CheckArmstrong.java

import java.util.Scanner;

public class CheckArmstrong {

      // method to check if a number is armstrong number

      static boolean isArmstrongNumber(int num) {

            // declaring sum of cubes to 0

            int sum = 0;

            // taking a copy of num

            int temp = num;

            // loops until temp becomes 0

            while (temp > 0) {

                  // extracting last digit

                  int digit = temp % 10;

                  // finding cube and adding to sum

                  sum += digit * digit * digit;

                  // removing last digit from temp

                  temp = temp / 10;

            }

            // at the end, if sum==num, it is an armstrong number

            if (sum == num) {

                  return true;

            }

            // else not

            return false;

      }

      public static void main(String[] args) {

            // declaring a Scanner, reading input, checking if input is an Armstrong

            // number

            Scanner scanner = new Scanner(System.in);

            System.out.println("Enter a number: ");

            int num = scanner.nextInt();

            if (isArmstrongNumber(num)) {

                  System.out.println(num + " is an Armstrong number");

            } else {

                  System.out.println(num + " is not an Armstrong number");

            }

      }

}

/*OUTPUT (multiple run)*/

Enter a number:

371

371 is an Armstrong number

Enter a number:

123

123 is not an Armstrong number

Add a comment
Know the answer?
Add Answer to:
Write a JAVA program to check whether a given number is an Armstrong number. An Armstrong...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer...

    Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371. Draw the flowchart and write a Java code to find ALL Armstrong number in the range of 0 and 999. You should write your code in two ways: (Yes as if you are...

  • C program--write a program to find whether the given number of three digits is an integer...

    C program--write a program to find whether the given number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is since 3**3 + 7**3 + 1**3 = 371. Output: Enter a number, or * to quit : 432 4**3 + 3**3 + 2**3 is not = 432 Enter a number, or * to quit : 371 3**3 + 7**3 + 1**3 is = 371...

  • Using C++ language please not matlab a. Write a function, called SumOfDigits that is able to...

    Using C++ language please not matlab a. Write a function, called SumOfDigits that is able to calculate the summation of a five-digit number. This function receives one integer parameter 'a', then returns the sum of 5 digits of the number. [2.5 marks] b. Write a function, called Armstrong that prints all Armstrong numbers in the range of 0 and 500. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is...

  • Armstrong numbers are numbers that are equal to the sum of their digits raised to power...

    Armstrong numbers are numbers that are equal to the sum of their digits raised to power of the number of digits in them. The number 153, for example, 1^3 + 5^3 + 3^3. Thus it is an Armstrong number. Write a program to display all three digit Armstrong numbers.

  • Write java code to create 3 methods that accomplish the below concepts according to simple number...

    Write java code to create 3 methods that accomplish the below concepts according to simple number theory. 1. Create a file PositiveInteger.java that houses a class called PositiveInteger 2. This class should have one single instance variable called num This is what I have for the constructor: public PositiveInteger(int number) { num = number; } The three methods should start with the italicized sections shown below to accomplish the corresponding tasks: 1. public boolean isPerfect () - A number is...

  • (Java Please) Sum of Digits Write a program that will sum the digits of a number...

    (Java Please) Sum of Digits Write a program that will sum the digits of a number input by the user. For example, if the user enters the number 1234, the sum of the digits will be 10 (1 + 2 + 3 + 4 = 10). The user will enter a number with at least 4 digits (greater than 1000). That value will be sent to a method which will return the sum. Sample Output 1: SUM OF DIGITS -------------...

  • 1. Write a program that takes a number as input and check whether the number is...

    1. Write a program that takes a number as input and check whether the number is positive, negative or zero. 2. Write a C++ program that prompts the user to enter a number and checks whether the entered number is even or odd. 3.  Using switch-case statement write a C++ program that prompts the user to enter 1, 2 or 3 and display "Red" if selection is 1, "Yellow" if selection is 2, or "Green" if selection is 3. 4. Write...

  • A java program Write a program that prompts for and reads in a positive    integer...

    A java program Write a program that prompts for and reads in a positive    integer into a variable n. Your program should then sum    the first n ODD integers and display the sum. For    example, if 3 is entered for n, your program should display    the the sum 1 + 3 + 5. If 5 is entered, your program should    display the sum 1 + 3 + 5 + 7 + 9. What is the...

  • Write java program to check that a (16-digit) credit card number is valid. A valid credit...

    Write java program to check that a (16-digit) credit card number is valid. A valid credit card number will yield a result divisible by 10 when you: Form the sum of all digits. Add to that sum every second digit, starting with the second digit from the right. Then add the number of digits in the second step that are greater than four. The result should be divisible by 10. For example, consider the number 4012 8888 8888 1881. The...

  • Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and re...

    Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and return their total. The main method then prints the digits total with proper label as shown below Method Sum )is of type integer and takes an integer value. The method recursively adds up the digits and returns their total. Document your code and use proper prompts for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT