Starting from:
$30

$24

CS Algorithms & Programming Lab 4 Solution

The objective of this lab is to learn how to use a while loop to implement automated repetition. Remember that analyzing your problems and designing them on a piece of paper before starting implementation/coding is always a best practice.

In this particular lab, only use the while loop, do not use the for or do-while loops.

0. Setup Workspace

Start VSC and open the previously created workspace named labs_ws. Now, under the labs folder, create a new folder named lab4.

In this lab, you are to have four Java classes/files (under labs/lab4 folder) as described below. A fifth and sixth Java file containing the revisions should go under this folder as well. We expect you to submit a total of 6 files including the revisions. Do not upload other/previous lab solutions in your submission. The user inputs in the sample runs are shown in blue.

For all parts of this assignment, you may assume that the user enters valid types and number of values (e.g. a positive integer when asked for a positive integer).

1. Fibonacci

Write a program that takes a value    from the user and finds the    th Fibonacci number.

Tip: In mathematics, the Fibonacci numbers, commonly denoted , form a sequence called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 = 0 and 1 = 1.

Sample runs:


Please input n: 5

Fib(5) is 5



Please input n: 12

Fib(12) is 144


2. Palindrome Check

Write a program that checks if the input string is a palindrome. Your program should take the string from the user and report if it is a palindrome or not.

Tip: A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as “madam” or “racecar”. Utility method charAt of the String class is of particular use for this problem.
Fun Fact: aibohphobia is the unofficial name of the fear of palindromes.

Sample runs:


Please input the string: aibohphobia

It is a palindrome.



Please input the string: not a palindrome It is not a palindrome.

3. Average, Minimum and Maximum Finder

Write a program that takes a number (positive integer) and reads positive integer values from the user, and finds the minimum and maximum values as well as the average of these integers.

Tip: Maximum and minimum values an integer can take are respectively Integer.MAX_VALUE and Integer.MIN_VALUE.


Sample run:


Please input n: 5

Now input 5 positive integers:
5
4
1
3
2
Min: 1
Max: 5
Average: 3.0


4. Text Art

Write a program that takes an integer number and prints the asterisk symbol (*) for lines where in each line, there are line number of Asterisks aligned left.

Sample run:


Enter number of lines: 5

*

**

***

****

More products