Week 4 Exercise
Week 4 Exercise
Ye
Week 4 Exercise
(Solution will not be provided)
1. Convert among 8-bit unsigned binary, decimal and hexadecimal representations of integers a and b:
a 1111 1111
b 43
2. Convert among decimal and 8-bit 2’s complement representations of integers a and b:
a -58
b 1000 0000
3. Write a program that reads from keyboard a positive integer N and output whether N is a prime number.
4. Write a program that prints the first 30 numbers of Fibonacci sequence on one line. The Fibonacci sequence is as
follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …
i.e. first two numbers are 0 and 1, each number after that is equal to the sum of the two numbers that precede it. For
example, the third number in the sequence is equal to the sum of the first and second number, the fourth number is
equal to the sum of the second and third number, and so on …
5. Write a program that repeatedly generates and prints random integers in the range [1, 100]. The program must
stop when the distance between two successive printed values is less than 5. For example:
-- The program can possibly output this: 76 42 45
-- The program can possibly output this: 43 77 94 54 66 64