Lab 3: There are 2 Parts to this assignment: PART 1: Create a program that asks
ID: 3591407 • Letter: L
Question
Lab 3: There are 2 Parts to this assignment: PART 1: Create a program that asks the user to enter a number from 1 to 20 Get the number from the user (For example if the user enters a 5, each loop below will repeat 5 times) Print the statement FAU Owls-Hoot Hoot" the number of times entered by the user using a while loop. Print the statement Programming is awesome the same number of times using a do/while loop. Print the statement "Florida beaches are beautiful" the same number of times using a for loop. Note: You will be graded on whether or not you used the correct loop as described above, not just on output PART 2: Using a while loop: Ask the user to enter a number until the user enters a -1 to stop, as long as the number is not-1, add the number to an ongoing sum After the user enters a -1 print the sum onto the screen and then print the message "Have a Nice Day:)" and end the programExplanation / Answer
//Below code is for Part 1 code is written in Java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
//Part 1 Starts
Scanner inputNumber=new Scanner(System.in);
System.out.println("Enter the number");
int userNumber=inputNumber.nextInt();
int temp=userNumber;
//Check if entered input is between 1 to 20
if(userNumber>0 && userNumber <21)
{
while(temp!=0)
{
System.out.println("FAU Owls - Hoot Hoot");
temp--;
}
temp=userNumber; // Assign number of users to variable temp to use in do while
do
{
System.out.println("Programming is awesome");
temp--;
} while (temp!=0);
temp=userNumber; // Assign number of users to variable temp to use in do while
for (int i = 0; i < temp; i++) {
System.out.println("Florida beaches are beaufiful");
}
}
else
{
System.out.println("Entered number is less than 1or greater than 20, Please enter the number between 1 to 20");
}
//Part 1 End
}
}
//Code for Part 1 finish
//Below code is for Part 2
import java.util.Scanner;
public class Program2 {
public static void main(String[] args) {
Scanner inputNumber=new Scanner(System.in); //To accept input
int userNumber,addition=0; //Initially addition is zero
while(true)
{
System.out.println("Enter the number, Enter -1 to exit");
userNumber=inputNumber.nextInt();
if(userNumber==-1)
{
break; //exit when received -1
}
else
{
addition=addition+userNumber; // Add the numbers
}
}
System.out.println("Sum is " + addition);
System.out.println("Have a nice day :) ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.