You are on page 1of 2

Lab week 5 (04/03-08/03/2013)

Question 1
Write a program that reads a digit from the keyboard (digit is an integer in between 0...9), and then convert it to text. A number outside of that range will terminate the program. (Hint: use switch statement)
Enter It is Enter It is Enter Press a digit: 0 the number zero a digit: 7 the number seven a digit: 34 any key to continue . . .

Question 2
Write a program that reads a non-negative integer from the board and compute the sum of all digits of that number. The program terminates when enter a negative integer. (Hint: use division and modulus operators and loop structure)
Enter a non-negative integer (negative integer to terminate): 234 The sum of all digits of the integer 234 is 9 Enter a non-negative integer (negative integer to terminate): 2045 The sum of all digits of the integer 2045 is 11 Enter a non-negative integer (negative integer to terminate): -1 Press any key to continue . . .

Question 3
Write a program to solve the equation + = given that , where and are positive integers read from keyboard. (Hint: use loop structure)
m = 1 n = 20 Solutions are: 3 4 5 5 12 13 6 8 10 8 15 17 9 12 15 12 16 20 Press any key to continue . . .

50

Question 4
Write a program that reads students marks. The process terminates when enter a number outside the range 010. After that, the program should display the number of students, the average mark of all students and student with highest and lowest mark.
Enter a students mark: 5.75 Enter a students mark: 7.25

Enter a students mark: 3.5 Enter a students mark: 11 The number of students is 3 The average mark is 5.5 Student 2 has the highest mark: 7.25 Student 2 has the highest mark: 7.25 Student 3 has the lowest mark: 3.5 Press any key to continue . . .

Question 5
Write a program that computes the average mark of N subjects for a student (N is a positive number read in from the keyboard). The mark for each subject must be between 010, otherwise the program will ask to re-enter that mark. Each subject has its own weight (1, 2, or 3)
Enter number of subjects to compute the average mark: 3 Enter mark for subject 1 (0 10): 5.5 Enter weight for subject 1 (1, 2, or 3): 2 Enter mark for subject 2 (0 10): 11 Enter mark for subject 2 (0 10): 10 Enter weight for subject 2 (1, 2, or 3): 1 Enter mark for subject 3 (0 10): 7.5 Enter weight for subject 3 (1, 2, or 3): 4 Enter weight for subject 3 (1, 2, or 3): 3 The average mark is 7.25 Press any key to continue . . .

Question 5
The Fibonacci sequence is as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21 The mathematics formula is: F(0) = 0; F(1) = 1; F(n) = F(n-1) + F(n-2). Write a program to compute and print out the first N number of this sequence (N read from the keyboard). (Hint: use for loop)
Enter a number N to compute the first N numbers in the Fibonacci sequence: 8 0, 1, 1, 2, 3, 5, 8, 13, Press any key to continue . . .

You might also like