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

Andrew Rosen Remember, if the problem description asks you to return something,

ID: 3872765 • Letter: A

Question

Andrew Rosen Remember, if the problem description asks you to return something, you don't print it out inside the method, you print out where the method was called and returned the value. 1 Armstrong Numbers An Armstrong Number is an integer such that the sum of the cube of each digit is equal to the whole number. Write a method that, given an integer as a parameter, returns whether or not a number is an Armstrong Number. Some examples of Armstrong Numbers: 371 3373 +1 370 = 33 + 73 + 03 2 Riddler Holy digits Batman! The Riddler is planning his next caper somewhere on Pennsylvania Avenue. In his usual sporting fashion, he has left the address in the form of a puzzle. The address on Pennsylvania is a four-digit number with the following properties . All four digits are different

Explanation / Answer

public class IntermediateLoopProblems {

/**

* @param args

*/

public static void main(String[] args) {

int n1 = 153, n2 = 344;

String str1 = "Raj Kumar";

if (isArmstrong(n1)) {

System.out.println(n1 + " is Armstrong");

} else {

System.out.println(n1 + " not is Armstrong");

}

if (isArmstrong(n2)) {

System.out.println(n2 + " is Armstrong");

} else {

System.out.println(n2 + " not is Armstrong");

}

System.out.println(str1 + " contains " + getNoOfVowels(str1)

+ " vowels");

}

/**

* method to check given number is armstrong or not

*

* @param n

* @return

*/

public static boolean isArmstrong(int n) {

int c = 0, a, temp;

temp = n;

while (n > 0) {

a = n % 10;

n = n / 10;

c = c + (a * a * a);

}

if (temp == c)

return true;

else

return false;

}

/**

* method to return number of vowels in the string

*

* @param str

* @return

*/

public static int getNoOfVowels(String str) {

int count = 0;

for (int i = 0; i < str.length(); i++) {

if (str.toUpperCase().charAt(i) == 'A'

|| str.toUpperCase().charAt(i) == 'E'

|| str.toUpperCase().charAt(i) == 'I'

|| str.toUpperCase().charAt(i) == 'O'

|| str.toUpperCase().charAt(i) == 'U')

count++;

}

return count;

}

}

OUTPUT:

153 is Armstrong
344 not is Armstrong
Raj Kumar contains 3 vowels