Assignment 02 - Branching
Assignment 02 - Branching
Branching
[You need to submit only the Home Tasks. Submit all the Homework Coding Tasks (Task 1 to
6) in the Google Form shared on buX. Submit all the Homework Tracing Tasks (Task 7 & 8)
handwritten to your Lab Instructors before the next lab.]
LAB Tasks
(No Need to Submit)
1. Write the Java code of a program to find the largest among three 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 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
4. Write a Java program that will take a year as input and print whether that year is a leap
year or not.
● A year may be a leap year if it is evenly divisible by 4.
● Years that are divisible by 100 (century years such as 1900 or 2100) cannot be
leap years unless they are also divisible by 400.
Design a Java code of a program that takes the value of x as user input and then displays
the output based on the given piecewise function.
Sample Input Output
-3 output: -6
1 output: 2
4 output: 15
10 output: 302
6. 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 }
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 }
Home Tasks
1. Write a Java code of a program that takes an integer number as user input and then
determines if that number is divisible by either 5 or 7 but not both; otherwise display
“No”. For example, numbers like 35, 70, 105, 140, 175, 210, 245, 280 etc. can be
divisible by both 5 and 7. These numbers are invalid.
15 Divisible by 5 Only
28 Divisible by 7 Only
36 No
2. 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.
4. 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.
5. Suppose you are hired by a supershop named Bastob. 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.
Enter the amount the customer need to pay(Taka) The returned amount is 18 taka.
35 100 taka note: 0
Enter the amount, customer gave(Taka) 50 taka note: 0
53 20 taka note: 0
10 taka note: 1
5 taka coin: 1
2 taka coin: 1
1 taka coin: 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
6. 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 nor different" otherwise.
Input the 1st number: 230 Neither all are equal or different
Input the 2nd number: 230
Input the 3rd number: 112
7. What will be the output of the following program? Your answer will not be accepted
without the workings.
1 public class Tracing2 {
12 var4 = true;
13 var5 = false;
15 result1 = (var1 && var2) && (40 % 3 > 45) || (var5 && var6);
30 }
31 }
8. What will be the output of the following program? Your answer will not be accepted
without the workings.
1 public class Tracing3 {
2 public static void main(String[] args) {
3 int p = 5;
4 int q = 6;
5 int r = 9;
6 int sum = 0;
7 if (p < 12) {
8 System.out.println(r + 2);
9 }
10 else {
11 System.out.println(r + p);
12 }
13 if (q > 20){
14 System.out.println(r + 19);
15 }
16 else if (q <= 6) {
17 System.out.println(q + 3);
18 }
19 else{
20 System.out.println(p + q + r);
21 }
22 if (r > 15) {
23 System.out.println(r);
24 }
25 else if (r == 0) {
26 System.out.println(p + q);
27 }
28 else {
29 System.out.println(p);
30 }
31 if (sum != 0) {
32 System.out.println(3);
33 }
34 else {
35 System.out.println(sum + 32);
36 }
37 if(p > 0 && r < 10){
38 System.out.println(p + r);
39 }
40 else {
41 System.out.println(p - r);
42 }
43 }
44 }