(I) SumAndAverage (Loop):
Write a program called SumAndAverage to produce the sum of 1, 2, 3, ..., to an upperbound (e.g., 100). Also compute and display the average. The output shall look like :The sum is 5050
The average is 50.5
Hints:
public class SumAndAverage { // saved as "SumAndAverage.java"
public static void main (String[] args) {
int sum = 0; // store the accumulated sum, init to 0
double average; // average in double
int lowerbound = 1; // the lower bound to sum
int upperbound = 100; // the upper bound to sum
for (int number = lowerbound; number <= upperbound; ++number) { // for loop
sum += number; // same as "sum = sum + number"
}
// Compute average in double. Beware that int/int produces int.
......
// Print sum and average.
......
}
}
Dowanload Sample Code - SumAndAverage.java
Try :
public static void main (String[] args) {
int sum = 0; // store the accumulated sum, init to 0
double average; // average in double
int lowerbound = 1; // the lower bound to sum
int upperbound = 100; // the upper bound to sum
for (int number = lowerbound; number <= upperbound; ++number) { // for loop
sum += number; // same as "sum = sum + number"
}
// Compute average in double. Beware that int/int produces int.
......
// Print sum and average.
......
}
}
Dowanload Sample Code - SumAndAverage.java
Try :
- Modify the program to use a "while-do" loop instead of "for" loop. int number = lowerbound; int sum = 0; while (number <= upperbound) { sum += number; ++number; }
- Modify the program to use a "do-while" loop. int number = lowerbound; int sum = 0; do { sum += number; ++number; } while (number <= upperbound);
- What is the difference between "for" and "while-do" loops? What is the difference between "while-do" and "do-while" loops?
- Modify the program to sum from 111 to 8899, and compute the average. Introduce an int variable called count to count the numbers in the specified range. int count = 0; // count the number within the range, init to 0 for (...; ...; ...) { ...... ++count; }
- Modify the program to sum only the odd numbers from 1 to 100, and compute the average. (Hint: n is an odd number if n % 2 is not 0.)
- Modify the program to sum those numbers from 1 to 100 that is divisible by 7, and compute the average.
- Modify the program to find the "sum of the squares" of all the numbers from 1 to 100, i.e. 1*1 + 2*2 + 3*3 + ... + 100*100.
=======================================================================
(II) Product1ToN (Loop):
Write a program called Product1ToN to compute the product of
integers 1 to 10 (i.e., 1×2×3×...×10). Try computing the product from 1 to 11, 1 to 12, 1 to 13 and 1 to 14.
Write down the product obtained and explain the results.
Hints: Declares an int variable called product (to accumulate the product) and initialize to 1.
Sample Code -
Product1ToN.java
======================================================================
Hint :
public class HarmonicSum { // saved as "HarmonicSum.java"
public static void main (String[] args) {
int maxDenominator = 50000;
double sumL2R = 0.0; // sum from left-to-right
double sumR2L = 0.0; // sum from right-to-left
// for-loop for summing from left-to-right
for (int denominator = 1; denominator <= maxDenominator; ++denominator) {
// Beware that int/int gives int.
}
// for-loop for summing from right-to-left
......
// Find the difference and display
......
}
}
Sample code - HarmonicSum.java
================================================================
JDK maintains the value of π in a
Hint: Add to sum if the denominator modulus 4 is 1, and subtract from sum if it is 3.
The average is 885.5
======================================================================
(III) HarmonicSum (Loop):
Write a program called HarmonicSum to compute the sum of a harmonic series, as shown below, where n=50000. The program shall compute the sum from left-to-right as well as from the right-to-left. Obtain the difference between these two sums and explain the difference. Which sum is more accurate?Hint :
public class HarmonicSum { // saved as "HarmonicSum.java"
public static void main (String[] args) {
int maxDenominator = 50000;
double sumL2R = 0.0; // sum from left-to-right
double sumR2L = 0.0; // sum from right-to-left
// for-loop for summing from left-to-right
for (int denominator = 1; denominator <= maxDenominator; ++denominator) {
// Beware that int/int gives int.
}
// for-loop for summing from right-to-left
......
// Find the difference and display
......
}
}
Sample code - HarmonicSum.java
================================================================
(IV) ComputePI (Loop & Condition):
Write a program calledComputePI
to compute the value of π, using the following series expansion. You
have to decide on the termination criterion used in the computation
(such as the number of terms used or the magnitude of an additional
term). Is this series suitable for computing π?double constant called Math.PI. Compare the values obtained and the Math.PI, in percents of Math.PI. Hint: Add to sum if the denominator modulus 4 is 1, and subtract from sum if it is 3.
double sum = 0;
int maxDenom = 10000000;
for (int denom = 1; ..... ; denom = denom + 2) {
if (denom % 4 == 1) {
sum += ......;
} else if (denom % 4 == 3) {
sum -= ......;
} else {
System.out.println("The computer has gone crazy?!");
}
}
Sample Code - ComputePI.java
================================================================
(V)CozaLozaWoza (Loop & Condition)****:
Very Interesting.
Write a program called CozaLozaWoza which prints the numbers 1 to 110, 11 numbers per line. The program shall print "Coza" in place of the numbers which are multiples of 3, "Loza" for multiples of 5, "Woza" for multiples of 7, "CozaLoza" for multiples of 3 and 5, and so on.
The output shall look like:
1 2 Coza 4 Loza Coza Woza 8 Coza Loza 11
Coza 13 Woza CozaLoza 16 17 Coza 19 Loza CozaWoza 22
23 Coza Loza 26 Coza Woza 29 CozaLoza 31 32 Coza
......
Note - You have to print just 11 numbers per line.
Hint -
public class CozaLozaWoza { // saved as "CozaLozaWoza.java"
public static void main(String[] args) {
int lowerbound = 1;
int upperbound = 110;
for (int number = lowerbound; number <= upperbound; ++number) {
// Print "Coza" if number is divisible by 3
if (......) {
System.out.print("Coza");
}
// Print "Loza" if number is divisible by 5
if (......) {
System.out.print(.....);
}
// Print "Woza" if number is divisible by 7
......
// Print the number if it is not divisible by 3, 5 and 7
if (......) {
......
}
// Print a space
......
// Print a newline if number is divisible by 11
if (......) {
System.out.println();
}
}
}
}
Try - Modify the program to use nested-if (if ... else if ... else if ... else) instead.
Sample Codes - CozaLozaWoza.java
======================================================================
(VI) Fibonacci (Loop):
Write a program called Fibonacci to display the first 20 Fibonacci numbers F(n), where F(n)=F(n–1)+F(n–2) and F(1)=F(2)=1.
Also compute their average. The output shall look like:
The first 20 Fibonacci numbers are:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 Also compute their average. The output shall look like:
The first 20 Fibonacci numbers are:
The average is 885.5
Hints - public class Fibonacci { public static void main (String args[]) { int n = 3; // the index n for F(n), starting from n=3 int fn; // F(n) to be computed int fnMinus1 = 1; // F(n-1), init to F(2) int fnMinus2 = 1; // F(n-2), init to F(1) int nMax = 20; // maximum n, inclusive int sum = fnMinus1 + fnMinus2; double average; System.out.println("The first " + nMax + " Fibonacci numbers are:"); ...... while (n <= nMax) { // Compute F(n), print it and add to sum ...... // Adjust the index n and shift the numbers ...... } // Compute and display the average (=sum/nMax) ...... } }
Sample Code - Fibonacci.java
========================================================================
(VI) Tribonacci (Loop):
Tribonacci numbers are a sequence of numbers T(n) similar to Fibonacci numbers, except that a number is formed by adding the three previous numbers, i.e., T(n)=T(n-1)+T(n-2)+T(n-3), T(1)=T(2)=1, and T(3)=2. Write a program called Tribonacci to produce the first twenty Tribonacci numbers.
Sample Code - Tribonacci.java
========================================================================
(VI) ExtractingDigits :
In this section of looping, this is going to be our last program and what it is?
Write a program to extract each digit from an int, in the reverse order. For example, if the int is 1542, the output shall be "2,4,5,1", with a comma separating the digits.
Hints: Use n % 10 to extract a digit; and n = n / 10 to discard the last digit.
int n = ....;
while (n > 0) {
int digit = n % 10; // Extract the last digit
......
.....
n = n / 10; // Drop last digit and repeat the loop
}

No comments:
Post a Comment