Description
Programming Assignment-III
(Conditional Statements)
1. Write a java program to input the height of the person and check if the height of the person
is greater than or equal to 6 feet then print the message “The person is tall”.
2. Write a java program to input the mark of a student and check if the student mark is greater than or equal to 40, then it generates the following message.
“Congratulation! You have passed the exam.”
Otherwise the output message is
“Sorry! You have failed the exam.”
3. Input an integer through the keyboard. Write a java program to find out whether it is an odd number or even number.
4. Any character is entered through the keyboard, write a java program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol. The following table shows the range of ASCII values for various characters.
Characters ASCII Values A – Z 65 – 90 a – z 97 – 122 0 – 9 48 – 57
special symbols 0 – 47, 58 – 64, 91 – 96, 123 – 127
5. The two roots of a quadratic equation 𝑎𝑥2 + 𝑏𝑥 + 𝑐 = 0 can be obtained using the following formula:
𝑟1 = −𝑏+√𝑏2−4𝑎𝑐 and 𝑟2 = −𝑏−√𝑏2−4𝑎𝑐
2𝑎 2𝑎
b2 – 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots.
Write a java program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is 0, display one root. Otherwise, display “The equation has no real roots”
Note that you can use Math.pow(x, 0.5) to compute
Here are some sample runs.
Enter a, b, c: 1.0 3 1
The equation has two roots -0.381966 and -2.61803
Enter a, b, c: 1 2.0 1
The equation has one root -1
Enter a, b, c: 1 2 3
The equation has no real roots
6. You can use Cramer’s rule to solve the following 2 X 2 system of linear equation:
𝑎𝑥 + 𝑏𝑦 = 𝑒
𝑐𝑥 + 𝑑𝑦 = 𝑓
𝑒𝑑 − 𝑏𝑓
𝑥 =
𝑎𝑑 − 𝑏𝑐
𝑎𝑓 − 𝑒𝑐
𝑦 =
𝑎𝑑 − 𝑏𝑐
Write a java program that prompts the user to enter a, b, c, d, e, and f and displays the result.
If ad – bc is 0, report that “The equation has no solution.”
Enter a, b, c, d, e, f: 9.0 4.0 3.0 -5.0 -6.0 -21.0 x is -2.0 and y is 3.0
Enter a, b, c, d, e, f: 1.0 2.0 2.0 4.0 4.0 5.0
The equation has no solution
7. Write a java program that takes the x – y coordinates of a point in the Cartesian plane and prints a message telling either an axis on which the point lies or the quadrant in which it is found.
Sample lines of output:
(-1.0, -2.5) is in quadrant III
(0.0, 4.8) is on the y-axis
8. If the ages of Rahul, Ayush and Ajay are input through the keyboard, write a java program to determine the elder among them.
9. Write a java program that randomly generates an integer between 1 and 12 and displays the
10. Write a java program that prompts the user to enter an integer for today’s day of the week
Here is a sample run:
Enter today’s day: 1
Enter the number of days elapsed since today: 3
Enter today’s day: 0
Enter the number of days elapsed since today: 31
11. The body mass index (BMI) is commonly used by health and nutrition professionals to estimate human body fat in populations. It is computed by taking the individual’s weight (mass) in kilograms and dividing it by the square of their height in meters. i.e.
𝑊𝑒𝑖𝑔ℎ𝑡 (𝑘𝑔)
𝑀𝑒𝑡𝑟𝑖𝑐: 𝐵𝑀𝐼 = ( 𝐻𝑒𝑖𝑔ℎ𝑡(𝑚))2
Write a java program by using some if statements to show the category for a given BMI.
BMI Category
less than 18.5 underweight
18.5 to 24.9 normal weight
25.0 to 29.9 overweight
30.0 or more obese
12. Write a java program that prompts the user to enter three integers and display the integers in non-decreasing order.
Here is a sample run:
Enter three integers: 2 4 3
Display the integers in non-decreasing order:
2 3 4
14. Write a java program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws.
Here are sample runs:
scissor (0), rock (1), paper (2): 1
The computer is scissor. You are rock. You won
scissor (0), rock (1), paper (2): 2
The computer is paper. You are paper too. It is a draw
15. Write a java program that prompts the user to enter a point (x, y) and checks whether the point is within the circle centered at (0, 0) with radius 10. For example, (4, 5) is inside the circle and (9, 9) is outside the circle,
(Hint: A point is in the circle if its distance to (0, 0) is less than or equal to 10. The formula for
computing the distance is √(𝑥2 − 𝑥1)2 + (𝑦2 − 𝑦1)2 .Test your program to cover all cases.)
Two sample runs are shown below.
Enter a point with two coordinates: 4 5
Point (4.0, 5.0) is in the circle
Enter a point with two coordinates: 9 9
Point (9.0, 9.0) is not in the circle
Mark Range Letter Grade
>=90 O
>=80 AND <90 A
>=70 AND <80 B
>=60 AND <70 C
>=50 AND <60 D
>=40 AND <50 E
<40 F
17. Write a java program that prompts the user to enter an integer and determines whether it is divisible by 5 and 6, whether it is divisible by 5 or 6, and whether it is divisible by 5 or 6, but not both.
Here is a sample run of this program:
Enter an integer: 10
Is 10 divisible by 5 and 6? false
Is 10 divisible by 5 or 6? true
Is 10 divisible by 5 or 6, but not both? True
18. Write a java program which displays an appropriate name for a person, using a combination of nested ifs and compound conditions. Ask the user for a gender, first name, last name and age. If the person is female and 20 or over, ask if she is married. If so, display “Mrs.” in front of her name. If not, display “Ms.” in front of her name. If the female is under 20, display her first and last name. If the person is male and 20 or over, display “Mr.” in front of his name. Otherwise, display his first and last name. Note that asking a person if they are married should only be done if they are female and 20 or older, which means you will have a single if and else nested inside one of your if statements. Also, did you know that with an if statements (or else), the curly braces are optional when there is only one statement inside?
What is your gender (M or F): F
First name: Gita
Last name: Pattanayak
Age: 32
Are you married, Gita (y or n)? y
Then I shall call you Mrs. Gita Pattanayak.
What is your gender (M or F): F
First name: Anjali
Last name: Mishra
Age: 48
Are you married, Anjali(y or n)? n Then I shall call you Ms. Anjali.
What is your gender (M or F): M
First name: Ashok
Last name: Mohanty
Age: 23
Then I shall call you Mr. Ashok.
What is your gender (M or F): M
First name: Rahul
Last name: Pati
Age: 15
Then I shall call you Rahul Pati
***************
Reviews
There are no reviews yet.