Java Reference
In-Depth Information
letters and outputs the corresponding telephone number in digits. If the
user enters more than eight letters, then process only the first seven letters.
Also, output the - (hyphen) after the third digit. Allow the user to use
uppercase and lowercase letters, as well as spaces between words. More-
over, your program should process as many telephone numbers as the user
wants. (Hint: You can read the entered telephone number as a string and
then use the charAt method of the class String to extract each char-
acter. For example, if str refers to a string, then the expression
str.charAt(i) returns the character at the ith position. Recall that in a
string, the position of the first character is 0 .)
6. Write a program that reads a set of integers, and then finds and prints the
sum of the even and odd integers.
7. Write a program that prompts the user to input a positive integer. It should
then output a message indicating whether the number is a prime number.
(Note: 2 is the only even number that is prime. An odd integer is prime if it is
not divisible by any odd integer less than or equal to the square root of the
number.)
5
Let n ¼ a k a k-1 a k-2 ...a 1 a 0 be an integer. Let t ¼ a 0 -a 1 +a 2 -...+(-1) k a k .
It is known that n is divisible by 11 if and only if t is divisible by 11. For
example, suppose that n ¼ 8784204. Then, t ¼ 4-0+2-4+8-7+8 ¼ 11.
Because 11 is divisible by 11, it follows that 8784204 is divisible by 11.
If n ¼ 54063297, then t ¼ 7-9+2-3+6-0+4-5 ¼ 2. Because 2 is not
divisible by 11, 54063297 is not divisible by 11.
Write a program that prompts the user to enter a positive integer and then
uses this criteria to determine whether the number is divisible by 11.
8.
9. Write a program that uses while loops to perform the following steps:
Prompt the user to input two integers: firstNum and secondNum .
( firstNum must be less than secondNum .)
a.
b. Output all
the odd numbers between firstNum and secondNum
inclusive.
c. Output the sum of all the even numbers between firstNum and
secondNum inclusive.
d. Output all the numbers and their squares between 1 and 10.
e. Output
the sum of
the squares of all
the odd numbers between
firstNum and secondNum inclusive.
f. Output all the uppercase letters.
10. Redo Exercise 9 using for loops.
Search WWH ::




Custom Search