Question 1
Write a program that reads from the user the height and radius of a
cylinder, and then outputs the area of this cylinder. The area of
the cylinder is calculated using the formula:
A=2*π*r*h+2*π*r2, where π = 3.14.
Sample run:
Enter the radius of the cylinder: 8.2
Enter the height of the cylinder: 9
The area of the cylinder is 885.75
Question 2
Write a program that reads the name, weight and height of a
patient. Then the program outputs if the patient is under weight,
average or over weight based on the following formula:
BMI = weight/height2
BMI < 20: Underweight
BMI between 20 and 25: Average
BMI > 25: Over weight
Sample run 1:
Enter your name: Samar
Enter your weight in kg: 70
Enter your height by meters: 1.67
Samar is overweight
Sample run 2:
Enter your name: Rola
Enter your weight in kg: 48
Enter your height by meters: 1.5
Rola is average
Sample run 3:
Enter your name: Rami
Enter your weight in kg: 85
Enter your height by meters: 1.88
Rami is underweight
Java language
Question 1:
Program: Cylinder.java
import java.util.Scanner;
public class Cylinder{
public static void main(String[] args){
Scanner sc = new Scanner(System.in); /* create Scanner object */
double radius; /* declare radius */
double height; /* declare height */
double pi = 3.14; /* declare and initialize pi */
System.out.print("Enter the radius of the cylinder: "); /* print message */
radius = sc.nextDouble(); /* read radius */
System.out.print("Enter the height of the cylinder: "); /* print message */
height = sc.nextDouble(); /* read height */
double area = 2*pi*radius*height + 2*pi*radius*radius; /* calculate area */
System.out.println("The area of the cylinder is " + area); /* print area */
}
}
Screenshot:
Output:
Question 2:
Program: BMI.java
import java.util.Scanner;
public class BMI{
public static void main(String[] args){
Scanner sc = new Scanner(System.in); /* create Scanner object */
double weight; /* declare weight */
double height; /* declare height */
double BMI; /* initialize BMI */
String name;
System.out.print("Enter your name: "); /* print message */
name = sc.nextLine(); /* read name */
System.out.print("Enter your weight in kg: "); /* print message */
weight = sc.nextDouble(); /* read weight */
System.out.print("Enter your height in meters: "); /* print message */
height = sc.nextDouble(); /* read height */
BMI = weight/(height*height); /* calculate BMI */
if(BMI<20){
System.out.println(name + " is underweight"); /* print underweight */
}
else if(BMI>25){
System.out.println(name +" is overweight"); /* print overweight */
}
else{
System.out.println(name + " is average"); /* print average */
}
}
}
Screenshot:
Output:
Please don't forget to give a Thumbs Up.
Get Answers For Free
Most questions answered within 1 hours.