Sie sind auf Seite 1von 2

# PROBLEMS (for Chapter 4)

IF STATEMENT

P1.1. Write a program that reads three nonzero numbers and determines and prints whether they
could represent the sides of a triangle.

P1.2. Write a program to calculate the average point from results of 5 subjects (Literature, Maths,
Physics, Chemistry, Biology and English) of a student.
1. If student belongs to group A, Maths, Physics and Chemistry get factor of 2, others get 1.
2. If student belongs to group B, Biology, Physics and Chemistry get factor of 2, others get 1.
3. If student belongs to group C or D, Maths, Literature and English get factor of 2, others get 1.
The average point is average of subjects after considering factors.

LOOPS

P2.1. Write a program that defines whether a number is a prime number or composite number. A
prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than
1 and itself. A natural number greater than 1 that is not a prime number is called a composite
number.
For example 5 is prime, as only 1 and 5 divide it, whereas 6 is composite, since it has the divisors 2
and 3 in addition to 1 and 6.

P2.2. Write a program to find the factorial of n, n is optional. Use 2 cases: for and while loop

P2.3. Write a program that uses a for structure to calculate and print the product of the odd integers
from 1 to n, n is optional.

P2.4. Drivers are concerned with the mileage obtained by their automobiles. One driver has kept
track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful.
Develop a program that use a while structure to input the miles driven and gallons used for each
tankful. The program should calculate and display the miles per gallon obtained for each tankful.
After processing all input information, the program should calculate and print the combined miles per
gallon obtained for all tankful.

P2.5. Develop a program that will determine whether a department store customer has exceeded the
credit limit on a charge account. For each customer, the following facts are available:
a. Account number
b. Balance at the beginning of the month
c. Total of all items charged by this customer this month
d. Total of all credits applied to this customer’s account this month
e. Allowed credit limit
The program should use a while structure to input each of these facts, calculate the new balance
(= beginning balance +charges-credits) and determine whether the new balance exceeds the
customer’s credit limit. For those customers whose credit limit is exceeded, the program should
display the customer’s account number, credit limit, new balance and the message “Credit limit
exceeded”.

## P2.6. Given the matrix A:

 3 5  4
A    8  1 33 
 17 6  9
Write a program that creates the array B by computing the natural logarithm of all the elements of A
whose value is no less than 1, and adding 20 to each element that is equal to or greater than 1.

SWITCH STATEMENT

P3.1. A mail order house sells five different products whose retail prices are: product 1-\$2.98,
product 2-\$4.50, product 3-\$9.98, product 4-\$4.49, and product 5-\$6.87. Write a program that reads
a series of pairs of numbers as follows:
a. Product number
b. Quantity sold for one day
Your program should use a switch statement to help determine the retail price for each product. Your
program should calculate and display the total retail value of all products sold last week.

P3.2. The following table gives the approximation values of the static coefficient of friction µ for
various materials.
Materials µ
Metal on metal 0.20
Wood on wood 0.35
Metal on wood 0.40
Rubber on concrete 0.70
To start a weight W moving on a horizontal surface, you must push with a force F, where F=µW. Write
a program that uses the switch structure to compute the force F. The program should accept as input
the value of W and the type of materials.