Write the Java program Use the appropriate loops and variables whenever possible
ID: 3827969 • Letter: W
Question
Write the Java program
Use the appropriate loops and variables whenever possible
Ask user to input his name
Ask user to input number between 1 and 10 (note: this is referred to as the user’s number in the instructions below)
Display the user’s name the number of times that he entered.
If the user’s name is “Bob” and he entered 3 then show
Bob
Bob
Bob
Ask the user to enter a number from 1 to 5. (this is referred to as the number the user chooses)
5 means that the user is finished with the program. The user should be able to make choices over and over until he chooses 5. The user will only be able to choose 5 once.
If the user chooses 1
Display all of the even numbers from 1 to 100
If the user chooses 2
Display the numbers from 1 to 50 in reverse order
50
49
48
etc.
If the user chooses 3
Display all the numbers from the user’s number to 99
If the user chooses 4
Display all the odd numbers from 1 to 50 multiplied by the user’s number
If the user chose 4
4
12
20
etc…
Note that 1 * 4 = 4, 3 * 4 = 12, 5 * 4 = 20, etc.
If the user chooses 5
If the user number is greater than 5, print this message
You picked a high number
Otherwise print this message
You picked a low number
Display this message (assuming the user’s name is Bob and the number he entered is 3)
Thank you, Bob, for using this program.
The number you entered is 3.
End of program.
Use the user’s name and number in your message.
Explanation / Answer
HI, Please find my implementation.
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter a number in range 1-10 : ");
int n = sc.nextInt();
for(int i=0; i<n; i++)
System.out.println(name);
System.out.print("Enter a number from 1 to 5: ");
int c = sc.nextInt();
while(c != 5){
switch (c) {
case 1:
for(int i=2; i<=100; i+=2)
System.out.print(i+" ");
System.out.println();
break;
case 2:
for(int i=50; i>=1; i--)
System.out.print(i+" ");
System.out.println();
break;
case 3:
for(int i=n; i<=99; i++)
System.out.print(i+" ");
System.out.println();
break;
case 4:
for(int i=1; i<=50; i+=2)
System.out.print(i+" ");
System.out.println();
break;
default:
if(c > 5)
System.out.println("You picked a high number");
else
System.out.println("You picked a low number");
break;
}
System.out.print("Enter a number from 1 to 5: ");
c = sc.nextInt();
}
System.out.println("Thank you, "+name+", for using this program.");
System.out.println("The number you entered is "+n+".");
System.out.println("End of program.");
}
}
/*
Sample run:
Enter your name: Pravesh
Enter a number in range 1-10 : 3
Pravesh
Pravesh
Pravesh
Enter a number from 1 to 5: 4
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Enter a number from 1 to 5: 8
You picked a high number
Enter a number from 1 to 5: -9
You picked a low number
Enter a number from 1 to 5: 5
Thank you, Pravesh, for using this program.
The number you entered is 3.
End of program.
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.