02 Java Control Flow Level 1 Lab Practice
02 Java Control Flow Level 1 Lab Practice
1. All values as variables including Fixed, User Inputs, and Results
2. Proper naming conventions for all variables
String name = "Eric";
double height = input.nextDouble();
double totalDistance = distanceFromToVia + distanceViaToFinalCity;
3. Proper Program Name and Class Name
4. Follow proper indentation
5. Give comments for every step or logical block like a variable declaration or conditional and
loop blocks
1. Sample Program 1 - Create a program to check if 3 values are internal angles of a triangle.
IMP => Follow Good Programming Practice demonstrated below in all Practice Programs
Hint =>
a. Get integer input for 3 variables named x, y, and z.
b. Find the sum of x, y, and z.
c. If the sum is equal to 180, print ”The given angles are internal angles of a
triangle” else print They are not
Java
// Creating Class with name TriangleChecker indicating the purpose is to
// check if the internal angles add to 180
import java.util.Scanner;
class TriangleChecker {
public static void main(String[] args) {
// Create a Scanner Object
Scanner input = new Scanner(System.in);
1
if (sumOfAngles == 180) {
System.out.println("The given angles are internal angles of a " +
"Triangle");
} else {
System.out.println("The given angles are not internal angles " +
"of a Triangle");
}
2. Sample Program 2 - Create a program to find the sum of all the digits of a number given by
a user.
Hint =>
a. Get an integer input for the number variable.
b. Create an integer variable sum with an initial value of 0.
c. Create a while loop to access each digit of the number.
d. Inside the loop, add each digit of the number to the sum.
e. Finally, print the sum outside the loop
Java
// Create SunOfDigit Class to compute the sum of all digits of a number
import java.util.Scanner;
class SumOfDigits {
2
// Run while loop to access each digit of number
while (number != 0) {
// Use number % 10 to find each digit of number from last
int digit = number % 10;
3
Level 1 Practice Programs
1. Write a program to check if a number is divisible by 5
I/P => number
O/P => Is the number ___ divisible by 5? ___
2. Write a program to check if the first is the smallest of the 3 numbers.
I/P => number1, number2, number3
O/P => Is the first number the smallest? ____
3. Write a program to check if the first, second, or third number is the largest of the three.
I/P => number1, number2, number3
O/P =>
Is the first number the largest? ____
Is the second number the largest? ___
Is the third number the largest? ___
4. Write a program to check for the natural number and write the sum of n natural numbers
Hint =>
a. A Natural Number is a positive integer (1,2,3, etc) sometimes with the inclusion of 0
b. A sum of n natural numbers is n * (n+1) / 2
I/P => number
O/P => If the number is a positive integer then the output is
The sum of ___ natural numbers is ___
Otherwise
The number ___ is not a natural number
5. Write a program to check whether a person can vote, depending on whether his/her age is
greater than or equal to 18.
Hint =>
a. Get integer input from the user and store it in the age variable.
b. If the person is 18 or older, print "The person can vote." Otherwise, print "The person
cannot vote."
I/P => age
O/P => If the person's age is greater or equal to 18 then the output is
The person's age is ___ and can vote.
Otherwise
The person's age is ___ and cannot vote.
4
6. Write a program to check whether a number is positive, negative, or zero.
Hint =>
a. Get integer input from the user and store it in the number variable.
b. If the number is positive, print positive.
c. If the number is negative, print negative.
d. If the number is zero, print zero.
7. Write a program SpringSeason that takes two int values month and day from the command
line and prints “Its a Spring Season” otherwise prints “Not a Spring Season”.
Hint =>
a. Spring Season is from March 20 to June 20
8. Write a program to count down the number from the user input value to 1 using a while loop
for a rocket launch
Hint =>
a. Create a variable counter to take user inputted value for the countdown.
b. Use the while loop to check if the counter is 1
c. Inside a while loop, print the value of the counter and decrement the counter.
9. Rewrite program 8 to do the countdown using the for-loop
10.Write a program to find the sum of numbers until the user enters 0
Hint =>
a. Create a variable total of type double initialize to 0.0. Also, create a variable to store the
double value the user enters
b. Use the while loop to check if the user entered is 0
c. If the user entered value is not 0 then inside the while block add user entered value to
the total and ask the user to input again
d. The loop will continue till the user enters zero and outside the loop display the total value
11.Rewrite the program 10 to find the sum until the user enters 0 or a negative number using
while loop and break statement
Hint =>
a. Use infinite while loop as in while (true)
b. Take the user entry and check if the user entered 0 or a negative number to break the
loop using break;
12.Write a program to find the sum of n natural numbers using while loop compare the result
with the formulae n*(n+1)/2 and show the result from both computations was correct.
Hint =>
a. Take the user input number and check whether it's a Natural number
b. If it's a natural number Compute using formulae as well as compute using while loop
c. Compare the two results and print the result
5
13.Rewrite the program number 12 with the for loop instead of a while loop to find the sum of n
Natural Numbers.
Hint =>
a. Take the user input number and check whether it's a Natural number
b. If it's a natural number Compute using formulae as well as compute using for loop
c. Compare the two results and print the result
14.Write a Program to find the factorial of an integer entered by the user.
Hint =>
a. For example, the factorial of 4 is 1 * 2 * 3 * 4 which is 24.
b. Take an integer input from the user and assign it to the variable. Check the user has
entered a positive integer.
c. Using a while loop, compute the factorial.
d. Print the factorial at the end.
15.Rewrite program 14 using for loop
Hint =>
a. Take the integer input, check for natural number and determine the factorial using for
loop and finally print the result.
16.Create a program to print odd and even numbers between 1 to the number entered by the
user.
Hint =>
a. Get an integer input from the user, assign to a variable number and check for Natural
Number
b. Using a for loop, iterate from 1 to the number
c. In each iteration of the loop, print the number is odd or even number
17.Create a program to find the bonus of employees based on their years of service.
Hint =>
a. Zara decided to give a bonus of 5% to employees whose year of service is more than 5
years.
b. Take salary and year of service in the year as input.
c. Print the bonus amount.
18.Create a program to find the multiplication table of a number entered by the user from 6 to 9.
Hint =>
a. Take integer input and store it in the variable number
b. Using a for loop, find the multiplication table of number from 6 to 9 and print it in the
format number * i = ___