Download Week 5 - NUS School of Computing

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Addition wikipedia , lookup

Halting problem wikipedia , lookup

Elementary mathematics wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Transcript
Questions for Discussion (Week 5)
Question 1:
Write a program that sums the integers within 100 (100 included), which are divisible by
3 or 5 but not both. That is, to compute 3 + 5 + 6 + 9 + 10 + 12 + 18 + 20 + 21 + …
Question 2:
Write a program to ask the user to input a sentence and count the number of vowels in the
sentence. The program should keep running until the user inputs "quit". You may assume
that all user inputs are in lower-case.
Question 3:
What are the outputs of the following code fragments? Comment on the style.
int x=5;
if (x==0)
if (true) System.out.print("A");
System.out.print("B");
for (int i=0; i<3; i++) {
System.out.print("C");
System.out.print("D");}
int counter=0;
for (int i=0; i< 5; i++) {
for (int j=0; j<i; j++) {
counter++;
}
}
System.out.print(counter);
1
Question 4:
Prime Factorisation
We would like to determine the prime factors of a positive number. A prime number has
two disctinct factors: 1 and itself. (The number 1 is not a prime). As an example, the
number 1960 consists of prime factors 2, 5 and 7, because 1960 is
222577
Write a program that takes in a positive integer greater than 1, and prints a summary of
the prime factors. The summary should list each prime factor, as well as the number of its
occurrences.
You may assume the user will enter an integer. However, your program should only
accept positive numbers greater than 1, and should keep asking until the user enters a
valid input.
Sample run (input)
Enter
Enter
Enter
Enter
a
a
a
a
positive
positive
positive
positive
integer
integer
integer
integer
greater
greater
greater
greater
Prime factor : 2
Occurrences : 3
Prime factor : 5
Occurrences : 1
Prime factor : 7
Occurrences : 2
2
than
than
than
than
1
1
1
1
:
:
:
:
-5
0
1
1960
Question 5
Write a program to calculate and display the bank balance at the end of every year, given
the number of years and the prevailing interest rate.
Your program should ask the user for an input number of years, and interest rate.
You may assume the following:
1. The inputs (number of years and interest rate) are positive integers.
2. Interest is compounded and calculated on a yearly basis.
3. Principle amount of money is deposited at the start of the year and interest is
paid at the end of the year.
4. Bank balance is an integer value (to simplify formatting of decimals display).
Sample run 1 (input):
Enter principle amount: 10000
Enter number of years: 3
Enter interest rate in percent (%): 5
The balance at the end of year 1 is $10500
The balance at the end of year 2 is $11025
The balance at the end of year 3 is $11576
Sample run 2 (input):
Enter principle amount: 500
Enter number of years: 4
Enter interest rate in percent (%): 3
The
The
The
The
balance
balance
balance
balance
at
at
at
at
the
the
the
the
end
end
end
end
of
of
of
of
year
year
year
year
3
1
2
3
4
is
is
is
is
$515
$530
$545
$561
Question 6
Write a program that takes in 2 positive numbers, N and M, and display the sum of all
factorials of the multiples of M that are less than N. Do not use any Math methods to
calculate factorial; use only repetition statements
i.e.
If N=10 and M=3, multiples of M that are less than N are: 3, 6, 9.
Sum of those factorials: 3! + 6! + 9! = 363606
You may assume the following:
1. N and M that the user inputs will not result in overflow
2. M is always less than N
3. N not larger than 13 (due to max int value)
Sample run 1 (input):
Enter N: 10
Enter M: 3
The result is: 363606
Sample run 2 (input):
Enter N: 12
Enter M: 2
The result is: 3669866
4