Java Reference
In-Depth Information
3. What will be printed by the following code snippet?
int i=0;
for (i=2;i
<
25; i+=2)
{
if ( i %7==0)
{
break ;
}
} System.out.println(i);
4. What will be printed by the following code snippet?
int i=0;
for (i=2;i < 25; i+=2) {
if ( i %7==0)
{
continue ;
}
} System.out.println(i);
5. Rewrite the following code to use a while statement instead of the for loop.
for ( int i=0,j=0;i < 10; i++) {
j ++;
System.out.println(i+j);
}
6. Write a program that persistently asks the user to enter a positive number. The
program should only terminate when a positive number is entered. Use a do-while
loop.
7. Rewrite the program from Question 6 to use an infinite while loop. That is,
while(true) {
structure. Use a break statement to terminate the loop when
a positive number is entered.
...
}
8. Write a program that reads an integer from the keyboard and tells the user if the
number is prime. Use a for loop to check if the number is divisible by the numbers
from 2 up to the value of the input number minus 1.
9. Write a program that reads an integer from the keyboard and prints the corresponding
binary number.
10. Write a program that reads an integer from the keyboard and prints the factorial of
the input. Use a variable of type long to store the result.
11. A palindrome is a string that reads the same forward and backward. Write a program
that reads a string from the keyboard and tells the user if the string is a palindrome.
3.10 Lab
You will create a two-player version of the Hangman game. The program should first
ask for the first player to enter a word and save the word. Next, the second player should
 
Search WWH ::




Custom Search