Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ask the user if they want to enter a number or if they want the computer to sele

ID: 3571595 • Letter: A

Question

Ask the user if they want to enter a number or if they want the computer to select a random number.

Based on the user selection, Ask the user for a number or generate a random number.

Read the number from the user (Skip the step if a random number is generated)

Check if the number is 1 digit or 2 digit or 3 digit or 4 digit

if it is 1 digit then check if the cube of number equals the number

else if it is 2 digit, get the cube of first and second digits and then sum them up and check if the number is equal to the sum.

else if it is 3 digit, get the cube of first, second and third digits and then sum them up and check if the number is equal to the sum.

if it is 4 digit, get the cube of first, second, third and fourth digits and then sum them up and check if the number is equal to the sum.

else tell the user that the number they have entered is not within 9999

Print a closing message saying if the number is an Armstrong number or not.

Print a goodbye statement.

Explanation / Answer

import java.util.Scanner; class tocheckArmstrongNumber { public static void main(String args[]) { int number, sum = 0, temporary, remainder, digits = 0; Scanner in = new Scanner(System.in); System.out.println("please enter a number to check if it is an Armstrong number"); number = in.nextInt(); temporary = n; // now we will count number of digits while (temporary != 0) { digits++; temporary = temporary/10; } temporary = n; while (temporary != 0) { remainder = temporary%10; sum = sum + powerfunction(remainder, digits); temporary = temporary/10; } if (number == sum) System.out.println(number + " It is an Armstrong number."); else System.out.println(number + "It is not an Armstrong number."); } static int powerfunction(int number, int r) { int c, z = 1; for (c = 1; c
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote