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: 3571594 • 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


package armstrongnumber;

import java.util.Random;
import java.util.Scanner;

public class ArmstrongNumber {
  
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int number;
//ask user choice
System.out.println("Do you want to enter a number or you want the computer to select a random number? <y/n> ");
String choice=input.nextLine();

//read input from user
if(choice.equalsIgnoreCase("y"))
{
System.out.println("Please enter the number: ");
number=input.nextInt();
}
else
{ // computer generates random number
Random r=new Random();
number=r.nextInt(10000);
System.out.println("Computer generated number: "+number);
}
double sum;
boolean flag=false;
//process the number
String num=""+number;
if(num.length()==1)
{
//if it is 1 digit then check if the cube of number equals the number
if(number==Math.pow(number,3))
{
flag=true;
}
}
else if(num.length()==2)
{
//get the cube of first and second digits and then sum them up and
//check if the number is equal to the sum.
sum=Math.pow(num.charAt(0),3)+Math.pow(num.charAt(1),3);
if(number==sum)
{
flag=true;
}
}
else if(num.length()==3)
{
//get the cube of first, second and third digits and then sum them up and check
//if the number is equal to the sum.
sum=Math.pow(num.charAt(0),3)+Math.pow(num.charAt(1),3)+Math.pow(num.charAt(2),3);
if(number==sum)
{
flag=true;
}
}
else if(num.length()==4)
{
//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.
sum=Math.pow(num.charAt(0),3)+Math.pow(num.charAt(1),3)+Math.pow(num.charAt(2),3)+Math.pow(num.charAt(3),3);
if(number==sum)
{
flag=true;
}
}
else
{
System.out.println("The number you have entered is not within 9999");
}

System.out.println(" Result");
if(flag==true)
System.out.println(number +" is an Armstrong number");
else
System.out.println(number +" is not an Armstrong number");
System.out.println("goodbye");

}
}
  

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