Assignment 3- Branching
Assignment 3- Branching
Branching
Beginner 4 40
Intermediate 6 60
Expert 5 50
Total 15 150
[Submit all the Coding Tasks (Task 1 to 13) in the Google Form shared on buX.
Submit the Tracing Tasks (Task 14 & 15) handwritten to your Lab Instructors
before the next lab.]
Beginner
1. Write the Java code of a program to find the largest among three different numbers
entered by the user.
5 largest number: 17
17
-5
2. Write a Java program that takes a student's numerical score as input as an integer and
prints their corresponding letter grade according to the following grading system:
Grades A A- B C D F
9 Your grade is F
82 Your grade is B
3. Write a calculator program in Java that takes two integers and an operator (+, -, *, /) as
input and performs the corresponding calculation.
Please use the equals( ) method for string comparison. You can check the hint below:
-99 -198
-99
+
10 3
3
/
4. Write a Java code of a program that takes an integer number as user input and then
determines if that number is divisible by both 5 and 7; otherwise display “No”. For
example, numbers like 35, 70, 105, 140, 175, 210, 245, 280 etc. can be divisible by both
5 and 7.
36 No
Intermediate
5. Write a Java program that will take a year as input and print whether that year is a leap
year or not.
6. Write a program which asks the user to enter an integer. Then you have to check the input
and based on that you have to print the following outputs.
-2 Number is negative
0 Number is zero
-3 output: -6
1 output: 2
4 output: 15
10 output: 302
8. Write a Java Program, that takes in a BRACU student ID as integer, and prints out the
year and the session the student enrolled in.
Hints:
● The first two digits denote the year the student got enrolled
●
The 3rd digit denotes the session Student joined(Spring1,Summer3,Fall2)
9. Write the Java code of a program that calculates the tax and prints it as follows:
a) No tax if you get paid less than 10,000
b) 5% tax if you get paid between 10,000 and 20,000 (both inclusive)
c) 10% tax if you get paid more than 20,000
d) NO TAX IF YOU ARE LESS THAN 18 YEARS OLD.
Hint: Take payment and age as user as inputs; then calculate tax and print it.
Sample Input Output
10. Write a Java program that takes 3 float numbers as input from the user and prints the
maximum and minimum number from the inputs.
11. A triangle has 3 sides. Write a program which asks the users for input. Based on the
input, your program should output whether it is an Equilateral, Isosceles or Scalene.
12. Suppose you are hired by Swapno. Now, your task is to create a Java program that will
help the cashier calculate the change to be returned.
The program takes two inputs. First input is an integer number which is the amount of
money to be paid in taka and the second integer is the amount of money the customer
gave to the cashier. Your program should print the following:
- If the customer gave more money than the actual amount, print change the cashier
should return in notes and coins.
- If the customer gave less money than the amount to be paid, then print the amount
the customer needs to pay.
-
Consider the following denomination for notes and coins in taka:
Notes: 100, 50, 20, 10.
Coins: 5, 2, 1.
Enter the amount the customer need to pay(Taka) The returned amount is 440 taka.
60 100 taka note: 4
Enter the amount, customer gave(Taka) 50 taka note: 0
500 20 taka note: 2
10 taka note: 0
5 taka coin: 0
2 taka coin: 0
1 taka coin: 0
Enter the amount the customer need to pay(Taka) The returned amount is 0 taka.
50
Enter the amount, customer gave(Taka)
50
Enter the amount the customer need to pay(Taka) Please pay 30 taka more.
550
Enter the amount, customer gave(Taka)
520
13. Write a Java program that reads three numbers and prints "All numbers are equal" if all
three numbers are equal, "All numbers are different" if all three numbers are different and
"Neither all are equal or different" otherwise.
Input the 1st number: 230 Neither all are equal or different
Input the 2nd number: 230
Input the 3rd number: 112
Tracing Tasks
14. What will be the output of the following program? Show the workings.
1 public class Tracing1 {
7 if (num3 < 0) {
8 System.out.println(num3 * (-2));
9 }
10 else {
11 System.out.println(sum);
12 }
13 if (num1 < 5) {
14 System.out.println(num1 + 10);
15 }
17 num2 = num1;
18 System.out.println(num2);
19 }
20 else {
22 }
23
24 System.out.println(num1);
25 }
26 if (num2 == 0) {
27 System.out.println(num2 + num3);
28 }
29 else {
30 System.out.println(num3);
31 }
32 if (sum != 0) {
33 System.out.println(100);
34 }
35 else {
36 System.out.println(sum + 100);
37 }
39 System.out.println(num1 == num2);
40 }
41 else {
42 System.out.println("End");
43 }
44 }
45 }
15. What will be the output of the following program? Your answer will not be accepted
without the workings.
12 var4 = true;
13 var5 = false;
15 result1 = (var1 && var2) && (40 % 3 > 45) || (var5 && var6);
30 }
31 }
Output: