IF STATEMENTS
Problem # 1
Write a program that reads
three integers numbers it then
prints the minimum number.
Problem # 2
Write a program that reads
three positive integers it then prints the difference between the maximum and
the minimum value.
Problem # 3
Write a program that reads 3
integers and prints the median number. The median number is defined to be the
number which is greater than one of the other numbers and less than the other
one.
Examples:
If the input is 20 17 65 the
median number is 20.
If the input is 12 12 12 the
median number is 12.
If the input is 18 7 18 the median number is 18.
Problem # 4
Write a program that reads the
coordinates X and Y of a point in a plane then it determines whether the point
is in quadrant 1, 2, 3 or 4 or whether it is the origin.
Problem # 5
Write a program that reads a
four digit integer representing a year it then it determines
whether the year is a leap
year or not. A year is leap if it is divisible by 4 and not by 100 or if it is
divisible by 400. Print the year that you entered and a message indicating
whether it is leap or not.
Problem # 6
Write a program which prompts
for and reads a positive integer N. It then determines whether the integer N is even or odd. The program
then prints one of the messages EVEN or ODD accordingly.
Problem # 7
Write a program that reads two
positive integers and then it determines whether any of the integers is
divisible by the other.
Problem # 8
Write a program which prompts
for and reads a positive integer N. It then determines whether the integer N is
divisible by 3 or not. The program then prints one of the messages DIVIBLE BY 3
or NOT DIVISIBLE BY 3 accordingly.
Problem # 9
Three positive integers J, K
and L form a pythagorean tripple if
J 2 + K 2 = L 2 . Write a program which
prompts for and reads three positive integers J, K, and L which are input in increasing
order. It then then determines whether the three integers form a
pythagorean tripple or not. The program then prints one of the messages
PYTHAGOREAN TRIPPLE or NOT A PYTHAGOREAN TRIPPLE accordingly.
Problem # 10
A salesperson is given commision on the following basis:
SALES |
COMMISSION |
Under 500 |
2 % of SALES |
500 and under 5000 |
5 % of SALES |
5000 and over |
8 % of SALES |
Write a program which reads
SALES and prints the corresponding commission.
Problem # 11
A salesman is given bonus as a
percentage of the sales he makes according to the following table:
SALES (RIYALS) |
BONUS |
SALES
< 1000 |
1 % OF SALES |
1000 <= SALES
< 3000 |
2 % OF SALES |
3000 <= SALES |
4 % OF SALES |
Write a main program to read the amount of sales and
print the bonus.
Use the following data:
2800.0
Problem # 12
The cost of shipping a parcel
to a destination in a particular zone is based on the following table:
ZONE CODE |
COST PER KG
(RIYALS) |
1 |
0.50 |
2 |
0.75 |
3 |
1.25 |
4 |
1.70 |
Write a program which reads a
zone code and the weight of a parcel in kilograms and prints the corresponding
shipping cost for that parcel.
Problem # 13
A telephone call from town A
to town B is charged 0.50 Riyals for any duration equal to or less than 3
minutes. Every additional minute is charged 0.75 Riyals. Write a program which
will read the duration of a call in minutes and then compute the charge for
that call.
Problem # 14
Write a program that reads the
coordinates of three ships and it then determines which two ships are closest.
Hint: If (X1, Y1) are the
coordinates of SHIP1 and (X2, Y2) are the coordinates of SHIP2, then the
distance separating the two ships is given by:
____________________
Ö (X1
- X2)2 + (Y1 - Y2)2
Problem # 15
Write a program to calculate
the excess baggage charge for a SAUDIA passenger. The free baggage allowance is
based on the following table:
CLASS |
CLASS CODE |
FREE BAGGAGE
ALLOWANCE (KILOGRAMS) |
FIRST |
1 |
40 |
HORIZON |
2 |
30 |
GUEST |
3 |
20 |
Excess baggage is charged at 1
% of the first class single fare per kilogram.
The input to the program is :
a class code, the passengers baggage weight in kilograms, and the first class
single fare for his flight.
The program must output:
Baggage weight, Excess bagage
weight, and excess baggage charge for the passenger.
Use the following data:
2 52 7348
Your output should be:
WEIGHT EXCESS
WEIGHT CHARGE
52
22 1616.5600586
Problem # 16
An employee is paid overtime
if he works more than 40 hours in a week. Write a program which reads the
number of hours (h ) an employee has worked in a week, the hourly rate of
payment (r) and then it computes the gross salary (g) and the net pay (p) as follows:
ì h * r if h £ 40
g = í
î 1.5r(h - 40) + 40r if h > 40
ì g if g £ 65.00
p = í
î g - (15.00 + 0.045g) if g
> 65.00
Your program should print the
hourly rate, the number of hours worked,
the gross salary, and the net pay.
Problem # 17
A function f(x,y) is defined as:
ì (x 2 + y 2 ) / (x - y) if x
¹ y
f(x,y) = í
î 2x 2 + y + 3 if x = y
Write a program to read X and Y and print the value of
f(X,Y).
Use the following data:
4.0 ,
2.0
4.1
Problem # 18
Three numbers A, B, and C form
the sides of a triangle if A < B + C,
B < A + C and C < A + B. Write a program which
reads three REAL numbers A, B and C it then determines whether the numbers form
a triangle or not. The program then either print the message : THE SIDES DO NOT
FORM A TRIANGLE or it calculates the area of the triangle using the formula:
S = (A + B + C) / 2
__________________________
AREA = Ö (S * (S - A) * (S - B) * (S - C) )
it then prints the area.
Problem # 19
Consider the set S defined as:
S: (x
- 2)2 + (y + 1)2 £ 4
Write a program which will read
the coordinates X and Y of a point and determine if the point belongs to the
set S.
Your output should be one of
the following messages:
THE
POINT BELONGS TO THE SET S
THE
POINT DOES NOT BELONG TO THE SET S
Problem # 20
Consider the three sets S1, S2, and S3 defined as:
S1: (x
- 2)2 + (y + 1)2 £ 4
S2: (x - 2)2 / 9 +
(y + 1)2 / 16 £ 1
S3: y £ 2x - 4
Write a program which will
read the coordinates X and Y of a point and determine if the point belongs to
the set S where:
S =
S1 Ç S2 Ç S3
Your output should be one of
the following messages:
THE
POINT BELONGS TO THE SET S
THE
POINT DOES NOT BELONG TO THE SET S
Problem # 21
Write a program that reads A,
B and C and then computes the value of X where:
______________
Ö A
- B +
2A3
X = ¾¾¾¾¾¾¾¾
2AC
the program should take care
of the problem of dividing by zero
or finding the square root of a negative number and it should print,
accordingly, one of the messages: DIVISION BY ZERO or NEGATIVE SQUARE ROOT if
one of these errors occurs. If both errors occur both messages should be
printed. If no error occurs, the program should print the value of X.
Problem # 22
SCECO charges its customers
according to the following rate:
0.30 Riyals per kilowatt-hour (kWh)
for electricity used up to the first 400 kWh
0.50 Riyals per kilowatt-hour
(kWh) for the next 600 kWh (up to 1000 kWh)
0.75 Riyals for all
electricity used over 1000 kWh.
Write a program which will
read the number of kilowatt-hours used by a customer and compute the total
charge for that customer.
Problem # 23
A teacher gives his class a
multiple-choice quiz consisting of 4 questions. Each question is worth 5
points. Each question has three choices: 1, 2, and 3. Write a program which reads
the correct answers for question 1, 2, 3, and 4 in the integer variables Q1,
Q2, Q3, and Q4 respectively. It then reads a student ID# and his answers for
question 1, 2, 3, and 4 respectively and computes and prints the student
percentage score.
Use the following data:
2 1 3 2
960002 3 1 3 2
Problem # 24
The charges of a private
hospital per day are as follows:
(a) Room
charges
(i) private
room: 125.00 Riyals
(ii) semi-private
room: 95.00 Riyals
(iii) ward: 75.00 Riyals
(b) Telephone charge: 1.75 Riyals
Write a program which will
read a data line, compute the patient’s bill, and print an appropriate
statement. A typical data line is:
5 ‘P’ ‘N’
where 5 indicates the number
of days spent in the hospital, ‘P’ represents the room type (‘P’ , ‘S’ , or
‘W’), ‘N’ represents the telephone option (‘Y’ for YES or ‘N’ for NO). A
statement for the given data is:
PATIENT
BILLING STATEMENT
NUMBER
OF DAYS IN HOSPITAL: 5
TYPE
OF ROOM: PRIVATE
ROOM
CHARGE: 625.0000000
TELEPHONE
CHARGE: 0.0000000
TOTAL
CHARGE: 625.0000000 RIYALS
**Problem # 25
Write a program that will
compute the net cost for a customer who is ordering one or more particular spare-part from a shop. The program should
prompt for, and read each of the following INTEGER input quantities:
Customer
Number
Spare-part
Number
Number
of spare-parts ordered
The prices per spare-part are
given in the following table:
Spare-part Number |
Price per spare-part (SR) |
200 |
80.0 |
300 |
100.0 |
400 |
150.0 |
The cost is computed as follows:
Total
cost = cost of spare-part(s) + transportation cost
transportation
cost = 6% of the cost of spare-part(s)
Net
cost = Total cost
- Discount
A discount of 20% ( of the total
cost) is given if the total cost is greater than SR 1000.0 otherwise no
discount is given.
Your output should be in the
form:
CUSTOMER
NUMBER ____________________
SPARE-PART
NUMBER ____________________
NUMBER
OF SPARE-PARTS ____________________
TOTAL
COST ____________________
DISCOUNT ____________________
NET
COST ____________________
Where the blank lines are replaced by appropriate numbers.
Note: Your program should
print the following error message:
INVALID
SPARE-PART NUMBER
if a spare-part number other
than 200, 300, or 400 is entered. It should also print the error message:
INVALID
NUMBER OF SPARE-PARTS
if a negative number is
entered for number of spare-parts. In both error cases the program should
terminate without computing the net cost.