For each of the following outputs, write a code segment using either a for or a
ID: 3632990 • Letter: F
Question
For each of the following outputs, write a code segment using either a for or a while loop that would produce it (you may need to use a nested loop). The code segment must output one value at every iteration and may not use arrays. For example, the output 0 1 2 3 4 5 can be produced by the following code segment:
int i = 0;
while (i < 6)
{
System.out.print(i + “ “);
i++;
}
Output 1: 1 3 5 7 9 11 13
Output 2: 13 10 7 4 1 -2
Output 3: 1 1 2 2 3 3 4 4 5 5
Output 4: 1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
Output 5: 1 2 4 7 11 16 22
2.) Write a program that generates a random number and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number.
3.) Write a program that asks the user for a positive integer no greater than 15. The program should then draw a square on an output file using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should write the following to the output file:
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
Explanation / Answer
Hope this helps, please rate! //Number 2 import java.util.Scanner; public class numberGuesser { /** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Guess a number between 1 and 100"); int guess = sc.nextInt(); int theNumber = (int) (Math.random()*100); while(guess!=theNumber && guess>0 && guesstheNumber){ System.out.println("Too high, try again."); } else if(guessRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.