Exercise 1. A. Write a program that will ask a user to enter the chance (percent
ID: 3594368 • Letter: E
Question
Exercise 1. A. Write a program that will ask a user to enter the chance (percentage) of rain as reported by the Weather Network. If the chance is 40% or higher, the program will tell the user to take an umbrella because there is a high chance of rain. If the chance of rain is less than 40% tell the user – Leave the umbrella at home. Test the program with two sets of data that show both outcomes and include the results in this report.
Sample output
Please type the chance of rain (percentage)%: 65
You should take your umbrella – there is a high chance of rain!
Please type the chance of rain (percentage)%: 30
Leave your umbrella at home.
Program:
Output (2 different outputs/tests – show both conditions):
Exercise 1. B. Write a program that prompts the user to enter two integers and determine if the first integer is divisible by the second (with no remainder). [Hint: modulus operator.] Test the program with two sets of data and include the results in this report to show both outcomes.
Some sample runs:
Please input 2 integers: 10 5
10 is divisible by 5
Please input 2 integers: 10 3
10 is not divisible by 3
Program:
Output (2 different outputs/tests that show all cases):
Make sure it is done in Java format.
Explanation / Answer
Program 1
import java.util.Scanner;
public class Sample
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Please type the chance of rain (percentage)%:");
int percentage = scanner.nextInt();
if(percentage >= 40) // If the chance is 40% or higher
System.out.println("You should take your umbrella – there is a high chance of rain!");
else
System.out.println("Leave your umbrella at home.");
}
}
Output
Please type the chance of rain (percentage)%: 65
You should take your umbrella – there is a high chance of rain!
Please type the chance of rain (percentage)%: 30
Leave your umbrella at home.
Program 2
import java.util.Scanner;
public class Sample
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in); //Scanner stores input from the system
int a = scanner.nextInt(); // nextInt() function reads the value of first integer and stores in local variable "a"
int b = scanner.nextInt(); // nextInt() function reads the value of second integer and stores in local variable "b"
if(a%b == 0) // Checks the modulous condition (with no remainder)
System.out.println(a+" is divisible by "+b); // Display statement by taking a and b values.
else // Remainder other than zero
System.out.println(a+" is not divisible by "+b);
}
}
Output
Please input 2 integers: 10 5
10 is divisible by 5
Please input 2 integers: 10 3
10 is not divisible by 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.