CSCI250 Exam I - Version 2 Spring 2 Problem 2: 120 pts An Armstrong number is a
ID: 3706884 • Letter: C
Question
CSCI250 Exam I - Version 2 Spring 2 Problem 2: 120 pts An Armstrong number is a 3-digit integer number for which the sum of cube of its digit is equal to itself Example of Armstrong number is 153 as 153-111+5+333. which is: 1+ 125+27 Another Armstrong number is 371. Write a Java program that checeks whether a number is an Armstrong number or not Sample Runl: Enter an integer number of 3 digits: 371 371 is an Armstrong number -Sample Run2: gits: 171 171 is not an Armstrong number Sampl nteger number of 3 digits: 2345 Invalid input!! Enter an integer number of 3 di le RuniExplanation / Answer
Answer:
//Java program for checking amstroArm number
import java.util.*;
class digits
{
private int num;
public void getNum(int x) //function to get value of num
{
num=x;
}//End of function getNum()
public boolean isAmstrong() //function to check amstrong
{
int n,sum,d;
n=num; // keep value of num
sum=0;
while (n>0)
{
d=n%10;
sum+=(d*d*d);
n/=10;
}
if(sum==num)
return true;
else
return false;
}
}
public class Amstrong
{
public static void main (String[] args)
{
digits dig=new digits();
int n;
Scanner sc=new Scanner(System.in);
System.out.print("enter an integer of 3 digits:"); // read number
n=sc.nextInt();
dig.getNum(n);
if(dig.isAmstrong())
{ System.out.println(n+"is an amstrong number");
}
else if
{ System.out.println(n+" is not an amstrong number");
}
else
{System.out.println("invalid input!");
}
}