0% found this document useful (0 votes)
3 views14 pages

Assignment 3- Branching

The document outlines a lab assignment for CSE110: Programming Language I, consisting of coding tasks categorized into beginner, intermediate, and expert levels, totaling 15 tasks worth 150 points. Students are required to submit coding tasks via a Google Form and handwritten tracing tasks to lab instructors. Each task includes specific programming challenges in Java, with sample inputs and outputs provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

Assignment 3- Branching

The document outlines a lab assignment for CSE110: Programming Language I, consisting of coding tasks categorized into beginner, intermediate, and expert levels, totaling 15 tasks worth 150 points. Students are required to submit coding tasks via a Google Form and handwritten tracing tasks to lab instructors. Each task includes specific programming challenges in Java, with sample inputs and outputs provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Lab Assignment 03

Branching

CSE110: Programming Language I

Difficulty No of Tasks Points to Score

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.

Sample Input Output

100 largest number: 100


23
-4

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:

Scores 90-100 85-89 70-84 57-69 50-56 <50

Grades A A- B C D F

Sample Input Output

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:

Sample Input Output

-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.

Sample Input Output

15 Invalid: Divisible by 5 Only

28 Invalid: Divisible by 7 Only


105 Divisible by Both

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.

● 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.

Sample Input Output

2020 2020 is a leap year

2001 2001 is not a leap year

1900 1900 is not a leap year

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.

● If the input is negative, then print “Number is negative”


● If the input is zero, then print “Number is zero”
● If the input is positive and even, then print “Number is positive and even”
● And if the input is positive and odd, then print “Number is positive and odd”

Sample Input Output

5 Number is positive and odd

-2 Number is negative

0 Number is zero

7. Let’s consider the following piecewise function:


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

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)

Sample Input Output

16101307 Student Joined BRAC in Spring 16

19301307 Student Joined BRAC in Summer 19

20201307 Student Joined BRAC in Fall 20

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

9000 Your tax amounts in 0 Tk


23

15000 Your tax amounts in 0 Tk


17

15000 Your tax amounts in 750 Tk


18

30000 Your tax amounts in 3000 Tk


25

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.

Sample Input Output

18.83 Maximum number is 83.12


-4.02 Minimum number is -4.02
83.12

26.45 Maximum number is 26.45


0.02 Minimum number is 0.02
13.56
Expert

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.

● Equilateral triangle has three sides with equal length


● Isosceles triangle has
two sides with equal length and another side is different
● Scalene triangle has different lengths in each side

Sample Input Output

5 This is a Scalene triangle


2
4

5 This is a Isosceles triangle


5
3

3 This is a Equilateral triangle


3
3

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.

Sample Input Output


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
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.

User Input Output

Input the 1st number: 2345 All numbers are different


Input the 2nd number: 2452
Input the 3rd number: 4532

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 {

2 public static void main(String[] args) { Output

3 int num1 = 10;

4 int num2 = -3;

5 int num3 = -1;

6 int sum = num1 + num2 + num3;

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 }

16 else if (num2 == -3) {

17 num2 = num1;

18 System.out.println(num2);

19 }

20 else {

21 System.out.println(num1 + num2 + num3);

22 }

23

if (num1 > 15) {

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 }

38 if (num1 > 0 && num2 < 0) {

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.

1 public class Tracing2 {

2 public static void main(String[] args) {

3 boolean var1, var2, var3, var4, var5, var6;

4 boolean result1, result2, result3, result4, result5;

5 boolean result6, result7, result8, result9, result10;

6 var1 = var2 = var3 = var4 = var5 = var6 = false;

7 result1 = result2 = result3 = result4 = result5 = false;

8 result6 = result7 = result8 = result9 = result10 = false;

9 var1 = (!false || false) && true;

10 var2 = var1 && true;

11 var3 = false && !true;

12 var4 = true;

13 var5 = false;

14 var6 = var3 && true;

15 result1 = (var1 && var2) && (40 % 3 > 45) || (var5 && var6);

16 result2 = (var1 || var2) || (result1 && false);

17 result3 = (var1 && result1) || result2 || var5;


18 result4 = (var1 || var2) || ((var3 && var1) && false);

19 result5 = (var1 && var2) && (result3 || var1);

20 result6 = ((var3 || !var2) && result5) || true;

21 result7 = (var4 && result1) && ((result1 && false) || true);

22 result8 = ((var1 && result3) && (!var5 || var6)) && true;

23 result9 = ((result2 && var2) || (!result7 && var1)) && !false;

24 result10 = !(var1 && true);

25 System.out.println(result1 + " " + result2);

26 System.out.println(result3 + " " + result4);

27 System.out.println(result5 + " " + result6);

28 System.out.println(result7 + " " + result8);

29 System.out.println(result9 + " " + result10);

30 }

31 }

Output:

You might also like