# Expert Work - Question 6.1, 6.2, 6.3, 6.6, 6.10

Question 6.1, 6.2, 6.3, 6.6, 6.10

6.1 (Math: pentagonal numbers) A pentagonal number is defined as n(3n–1)/2 for n = 1, 2, . . ., and so on. Therefore, the first few numbers are 1, 5, 12, 22, . . . . Write a method with the following header that returns a pentagonal number:

public static int getPentagonalNumber(int n) Write a test program that uses this method to display the first 100 pentagonal numbers with 10 numbers on each line.

*6.2 (Sum the digits in an integer) Write a method that computes the sum of the digits in an integer. Use the following method header:

public static int sumDigits(long n) For example, sumDigits(234) returns 9(2 + 3 + 4). (Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10 (= 4). To remove 4 from 234, use 234 / 10 (= 23). Use a loop to repeatedly extract and remove the digit until all the digits are extracted. Write a test program that prompts the user to enter an integer and displays the sum of all its digits.

**6.3 (Palindrome integer) Write the methods with the following headers // Return the reversal of an integer, i.e., reverse(456) returns 654 public static int reverse(int number)

// Return true if number is a palindrome public static boolean isPalindrome(int number) Use the reverse method to implement isPalindrome. A number is a palindrome if its reversal is the same as itself. Write a test program that prompts the user to enter an integer and reports whether the integer is a palindrome.

6.6 (Display patterns) Write a method to display a pattern as follows:

1

2 1

3 2 1

... n n-1 ..3 2 1

The method header is

public static void displayPattern(int n).

6.10

write a progra whether a number isPrime(int number) method for testing whether a number is prime. Use this method to find the number of prime numbers less than 10000.

6.1 (Math: pentagonal numbers) A pentagonal number is defined as n(3n–1)/2 for n = 1, 2, . . ., and so on. Therefore, the first few numbers are 1, 5, 12, 22, . . . . Write a method with the following header that returns a pentagonal number:

public static int getPentagonalNumber(int n) Write a test program that uses this method to display the first 100 pentagonal numbers with 10 numbers on each line.

*6.2 (Sum the digits in an integer) Write a method that computes the sum of the digits in an integer. Use the following method header:

public static int sumDigits(long n) For example, sumDigits(234) returns 9(2 + 3 + 4). (Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10 (= 4). To remove 4 from 234, use 234 / 10 (= 23). Use a loop to repeatedly extract and remove the digit until all the digits are extracted. Write a test program that prompts the user to enter an integer and displays the sum of all its digits.

**6.3 (Palindrome integer) Write the methods with the following headers // Return the reversal of an integer, i.e., reverse(456) returns 654 public static int reverse(int number)

// Return true if number is a palindrome public static boolean isPalindrome(int number) Use the reverse method to implement isPalindrome. A number is a palindrome if its reversal is the same as itself. Write a test program that prompts the user to enter an integer and reports whether the integer is a palindrome.

6.6 (Display patterns) Write a method to display a pattern as follows:

1

2 1

3 2 1

... n n-1 ..3 2 1

The method header is

public static void displayPattern(int n).

6.10

write a progra whether a number isPrime(int number) method for testing whether a number is prime. Use this method to find the number of prime numbers less than 10000.

You'll get a 1.1KB .ZIP file.