PJLTutorial4

advertisement

EE20084 Tutorial 4

Q1Running total.

Write a program that will keep a running total of numbers as the user enters them one by one. Your program should print the total after a number is entered and terminate when the user enters the number 0.

Q2 Wondrous numbers

“Wondrous numbers are defined like this: Start with an arbitrary natural number

(integer greater than zero). If the number is even, divide it by 2. If it is odd, multiply by 3 and add 1. Repeat until you come out with 1. A number is wondrous if and only if it eventually reaches 1 through this process.” http://mathforum.org/library/drmath/view/57184.htm

Write a C program that will work out if a number is wondrous.

Add the following features:

Display how many steps it takes to reach 1.

Give up after a given number of iterations

Test all the numbers between 1 and 1000 to see if any of them are unwondrous. o What number in this range takes the most steps to reach one? o What number has the highest value during the process?

Q3 Convert from an arbitrary base

.

Write a program that will convert from any base (0-9) to decimal e.g.

Enter a number and its base: 1100 2

1100 is 12 in decimal

Q4 Convert from an decimal to an arbitrary base.

Write a program that will convert a decimal into any base (0-9) e.g.

Enter a number and the target base: 12 2

12 is 1100 in binary.

Download