Programming Fundamentals Assignments
Programming Fundamentals Assignments
Share Link:
https://goo.gl/E7X1z3
A. Print following patterns on screen with appropriate logic:
1.
1
12
123
1234
12345
2.
12345
1234
123
12
1
3.
*
**
***
****
*****
4.
12345
2345
345
Page 1 of 5
45
5
Page 2 of 5
D. Write Java programs for the following case studies:
Case Study -1
Suppose you want to develop a program that changes a given amount of money into smaller
monetary units. The program lets the user enter an amount as a double value representing a
total in dollars and cents, and outputs a report listing the monetary equivalent in the maximum
number of dollars, quarters, dimes, nickels, and pennies, in this order, to result in the minimum number
of coins.
Here are the steps in developing the program:
1. Prompt the user to enter the amount as a decimal number, such as 11.56.
2. Convert the amount (e.g., 11.56) into cents (1156).
3. Divide the cents by 100to find the number of dollars. Obtain the remaining cents using
the cents remainder 100.
4. Divide the remaining cents by 25 to find the number of quarters. Obtain the remaining
cents using the remaining cents remainder 25.
5. Divide the remaining cents by 10 to find the number of dimes. Obtain the remaining
cents using the remaining cents remainder 10.
6. Divide the remaining cents by 5to find the number of nickels. Obtain the remaining
cents using the remaining cents remainder 5.
7. The remaining cents are the pennies.
8. Display the result.
You can find out the date of the month when your friend was born by asking five questions.
Each question asks whether the day is in one of the five sets of numbers.
Key
Page 3 of 5
The birthday is the sum of the first numbers in the sets where the day appears. For example,
if the birthday is 19, it appears in Set1, Set2, and Set5. The first numbers in these three sets
are1,2, and 16. Their sum is 19.
Listing 4.3 gives a program that prompts the user to answer whether the day is in Set1
(lines 41–44), in Set2 (lines 50–53), in Set3 (lines 59–62), in Set4 (lines 68–71), and in Set5
(lines 77–80). If the number is in the set, the program adds the first number in the set to day
(lines 47, 56, 65, 74, 83)
The greatest common divisor (gcd) of the two integers 4and 2is 2. The greatest common
divisor of the two integers 16 and 24is 8. How would you write this program to find the greatest common
divisor? Would you immediately begin to write the code? No. It is important to
think before you code. Thinking enables you to generate a logical solution for the problem
without concern about how to write the code.
Let the two input integers be n1 and n2. You know that number 1 is a common divisor, but
it may not be the greatest common divisor. So, you can check whether k(for k=2,3,4, and
so on) is a common divisor for n1 and n2, until kis greater than n1 or n2. Store the common
divisor in a variable named gcd. Initially, gcd is 1. Whenever a new common divisor is found,
it becomes the new gcd. When you have checked all the possible common divisors from 2up
To n1 or n2, the value in variable gcd is the greatest common divisor. Once you have a logical
solution, type the code to translate the solution into a Java program as follows:
Int gcd = 1;// Initial gcd is 1
intk = 2;// Possible gcd
while(k <= n1 && k <= n2) {
if(n1 % k == 0&& n2 % k == 0)
gcd = k; // Update gcd
k++; // Next possible gcd
}
// After the loop, gcd is the greatest common divisor for n1 and n2
Case Study – 5(Analyzing Numbers(Arrays))
The problem is to read 100 numbers, get the average of these numbers, and find the
number of the items greater than the average. To be flexible for handling any number of input, we
Page 4 of 5