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

Control Flow Level 2

The document contains a series of Java programming exercises that involve basic programming concepts such as loops, conditionals, and user input. Each exercise includes a description of the task, hints for implementation, and the corresponding Java code. The tasks range from printing odd and even numbers, calculating employee bonuses, generating multiplication tables, to finding factors and powers of numbers.

Uploaded by

vik737610
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

Control Flow Level 2

The document contains a series of Java programming exercises that involve basic programming concepts such as loops, conditionals, and user input. Each exercise includes a description of the task, hints for implementation, and the corresponding Java code. The tasks range from printing odd and even numbers, calculating employee bonuses, generating multiplication tables, to finding factors and powers of numbers.

Uploaded by

vik737610
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

Level 2 Practice Programs

1. 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
Code:
import [Link];

public class OddEvenPrinter {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter a positive integer: ");


int number = [Link]();

if (number <= 0) {
[Link]("Please enter a positive integer.");
} else {
[Link]("Odd and even numbers between 1 and " + number + ":");
for (int i = 1; i <= number; i++) {
if (i % 2 == 0) {
[Link](i + " is an even number");
} else {
[Link](i + " is an odd number");
}
}
}

[Link]();
}
}
[Link] 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.
Code:
import [Link];

public class EmployeeBonus {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter the employee's salary: ");


double salary = [Link]();

[Link]("Enter the employee's years of service: ");


int yearsOfService = [Link]();

double bonus = 0;

if (yearsOfService > 5) {
bonus = salary * 0.05; // 5% bonus
}

[Link]("The bonus amount is: $%.2f%n", bonus);

[Link]();
}
}

[Link] 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 = ___
Code:
import [Link];

public class MultiplicationTable {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");


int number = [Link]();

[Link]("Multiplication table of " + number + " from 6 to 9:");

for (int i = 6; i <= 9; i++) {


int result = number * i;
[Link](number + " * " + i + " = " + result);
}

[Link]();
}
}
[Link] a program FizzBuzz, take a number as user input, and if it is a positive integer loop
from 0 to the number and print the number, but for multiples of 3 print "Fizz" instead of the
number, for multiples of 5 print "Buzz", and for multiples of both print "FizzBuzz".
Hint =>
a. Write the program and use for loop
Code:
import [Link];

public class FizzBuzz {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter a positive integer: ");


int number = [Link]();

if (number <= 0) {
[Link]("Please enter a positive integer.");
} else {
for (int i = 1; i <= number; i++) {
if (i % 3 == 0 && i % 5 == 0) {
[Link]("FizzBuzz");
} else if (i % 3 == 0) {
[Link]("Fizz");
} else if (i % 5 == 0) {
[Link]("Buzz");
} else {
[Link](i);
}
}
}

[Link]();
}
}

[Link] the program 5 FizzBuzz using while loop


Code:
import [Link];

public class FizzBuzzWhile {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter a positive integer: ");


int number = [Link]();

if (number <= 0) {
[Link]("Please enter a positive integer.");
} else {
int i = 1;
while (i <= number) {
if (i % 3 == 0 && i % 5 == 0) {
[Link]("FizzBuzz");
} else if (i % 3 == 0) {
[Link]("Fizz");
} else if (i % 5 == 0) {
[Link]("Buzz");
} else {
[Link](i);
}
i++;
}
}

[Link]();
}
}
[Link] a program to find the youngest friends among 3 Amar, Akbar, and Anthony based
on their ages and the tallest among the friends based on their heights
Hint =>
a. Take user input for the age and height of the 3 friends and store it in a variable
b. Find the smallest of the 3 ages to find the youngest friend and display it
c. Find the largest of the 3 heights to find the tallest friend and display it
Code:
import [Link];

public class FriendComparison {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

// Input for Amar


[Link]("Enter Amar's age: ");
int amarAge = [Link]();
[Link]("Enter Amar's height (in cm): ");
int amarHeight = [Link]();

// Input for Akbar


[Link]("Enter Akbar's age: ");
int akbarAge = [Link]();
[Link]("Enter Akbar's height (in cm): ");
int akbarHeight = [Link]();

// Input for Anthony


[Link]("Enter Anthony's age: ");
int anthonyAge = [Link]();
[Link]("Enter Anthony's height (in cm): ");
int anthonyHeight = [Link]();

// Find the youngest


String youngest = "Amar";
int youngestAge = amarAge;

if (akbarAge < youngestAge) {


youngest = "Akbar";
youngestAge = akbarAge;
}
if (anthonyAge < youngestAge) {
youngest = "Anthony";
youngestAge = anthonyAge;
}

// Find the tallest


String tallest = "Amar";
int tallestHeight = amarHeight;

if (akbarHeight > tallestHeight) {


tallest = "Akbar";
tallestHeight = akbarHeight;
}
if (anthonyHeight > tallestHeight) {
tallest = "Anthony";
tallestHeight = anthonyHeight;
}

// Display results
[Link]("The youngest friend is " + youngest + " at " + youngestAge +
" years old.");
[Link]("The tallest friend is " + tallest + " at " + tallestHeight + " cm
tall.");

[Link]();
}
}

[Link] a program to find the factors of a number taken as user input.


Hint =>
a. Get input value for a variable named number.
b. Run a for loop from i = 1 to i < number. In each iteration of the loop, check if the
number is perfectly divisible by i. If true, print the value of i.
Code:
import [Link];

public class FactorFinder {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter a positive integer: ");


int number = [Link]();

if (number <= 0) {
[Link]("Please enter a positive integer.");
} else {
[Link]("Factors of " + number + " are:");
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
[Link](i + " ");
}
}
}

[Link]();
}
}

[Link] a program to print the greatest factor of a number beside itself using a loop.
Hint =>
a. Get an integer input and assign it to the number variable. As well as define a
greatestFactor variable and assign it to 1
b. Create a for loop that runs from last but one till 1 as in i = number - 1 to i = 1.
c. Inside the loop, check if the number is perfectly divisible by i then assign i to
greatestFactor variable and break the loop.
d. Display the greatestFactor variable outside the loop
Code:
import [Link];

public class GreatestFactor {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a positive integer: ");
int number = [Link]();
int greatestFactor = 1;

if (number <= 1) {
[Link]("Please enter a number greater than 1.");
} else {
for (int i = number - 1; i > 1; i--) {
if (number % i == 0) {
greatestFactor = i;
break;
}
}
[Link]("The greatest factor of " + number + " besides itself is: " +
greatestFactor);
}

[Link]();
}
}

[Link] a program to find the power of a number.


Hint =>
a. Get integer input for two variables named number and power.
b. Create a result variable with an initial value of 1.
c. Run a for loop from i = 1 to i <= power.
d. In each iteration of the loop, multiply the result with the number and assign the value
to the result.
e. Finally, print the result
Code:
import [Link];

public class PowerCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter the base number: ");


int number = [Link]();

[Link]("Enter the power: ");


int power = [Link]();

int result = 1;

for (int i = 1; i <= power; i++) {


result *= number;
}

[Link](number + " raised to the power of " + power + " is: " + result);

[Link]();
}
}

[Link] a program to find all the multiple of a number taken as user input below 100.
Hint =>
a. Get input value for a variable named number.
b. Run a for loop backwards: from i = 100 to i = 1.
c. Inside the loop, check if i perfectly divides number.
d. If true, print the number and continue the loop.
Code:
import [Link];

public class MultiplesBelow100 {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter a positive integer: ");


int number = [Link]();

if (number <= 0) {
[Link]("Please enter a positive integer.");
} else {
[Link]("Multiples of " + number + " below 100 are:");
for (int i = 100; i >= 1; i--) {
if (i % number == 0) {
[Link](i + " ");
}
}
}

[Link]();
}
}

You might also like