0% found this document useful (0 votes)
4 views

Algorithm Trials

The document outlines a series of computer programming tasks related to various mathematical and logical problems, including calculating employee net pay, interchanging variable values, finding occurrences of numbers, computing statistical measures, and determining properties of numbers. Each question provides a specific problem statement along with a proposed solution structure in pseudocode. The tasks cover a range of topics suitable for programming practice, including loops, conditionals, and arithmetic operations.

Uploaded by

OddWarrior
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Algorithm Trials

The document outlines a series of computer programming tasks related to various mathematical and logical problems, including calculating employee net pay, interchanging variable values, finding occurrences of numbers, computing statistical measures, and determining properties of numbers. Each question provides a specific problem statement along with a proposed solution structure in pseudocode. The tasks cover a range of topics suitable for programming practice, including loops, conditionals, and arithmetic operations.

Uploaded by

OddWarrior
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Question 1

Employees of JB and Sons Consultants Ltd are paid on an hourly basis at the end of every week.
If an employee works for not more than 40 hours a week, it is considered regular and Overtime
for hours worked in excess of 40. Regular hours are paid at 500 cedis and 550 cedis per hour for
males and females respectively while the overtime rate is one and half times the regular rate per
hour for the different sexes. All employees are to pay 15% of their gross pay as Income Tax,
2.5% as National Health Contribution Levy, 1% as District Tax. Employees who have more than
three children are to pay 10 and 20 cedis per child in excess of three towards Educational Fund
For All for males and females respectively. Devise a computer solution that can be used to
calculate the Net Pay of employees.
IPUT GENDER
INPUT
INPUT hour
IF(hours <= 40)
IF (female) THEN
Grosspay = 550 * 40
ELSE Grosspay = 500 * 40
END IF
END IF
IF (hours > 40)
grossPay=(hours *40) * 825+(550 * 40)
ELSE Grosspay = (500 * 40) + (hours - 40) * 750
END IF
END IF
IncomeTax = 0.15 * Grosspay
HealthTax = 0.025 * Grosspay
DistrictTax = 0.01 * Grosspay
IF (children >= 3) THEN
Edufund = 10 + (children - 3) * 20
ELSE Edufund = 0
END IF
NetPay = Grosspay - (IncomeTax + HealthTax + DistrictTax + Edufund)

Question 2
Devise a computer solution that interchanges the values of two variables, X and Y. The original
values of X and Y will be entered via the keyboard. Test your solution with initial values of X
and Y being 10 and 20 respectively. At the end of the test, do you have X and Y to be 20 and 10
respectively? If yes then your computer solution may be correct.

Declare A B C AS Int
INPUT A
INPUT B
IF A > B THEN
C=A
A=B
B=C
END IF
PRINT A
Question 3
Devise a computer solution that can be used to find the position of the last occurrence of a given
number from a given set of input. You may 'dry run' your solution with the following test data.
02425682634563227891221
Let 2 be the number that we want to find the position of its last occurrence (this should be 22)
DECLARE NumNum Numfind count NoNumfind
INPUT NumNUm
INPUT Numfind
count = 0
NoNumfind = 0
WHILE (count < NumNUm)
INPUT Num
count = count + 1
IF (Num = Numfind) THEN
NoNumfind = NoNumfind + 1
END IF
WEND
PRINT NoNumfind; "is the Number of times"; Numfind, "appears in the digit"
Question 4
Device a computer solution that can be used to compute the median and the mean of a set of
numbers given that the numbers have already been arranged in decreasing order of magnitude.
INPUT n
Declare array(n) count sum mean median AS INTEGER
count = 0
sum = 0
WHILE (count < n)
INPUT Num
count = count + 1
sum = sum + Num
END WILE
Mean = sum / n
IF n MOD 2 = 0 THEN
medianPOS = (Num(n / 2 - 1) + Num(n / 2)) / 2
ELSE median = Num * (n / 2)
END IF
PRINT median; "is the median"
PRINT Mean; "is the mean"
END

Question 5
Devise a computer solution that reads in a positive integer n and then prints the sum of the first n
even integers and the sum of the first n odd numbers.
Declare NumNum SumOdd SumEven numOdd numEven total
INPUT n
SumOddd = 0
SumEven = 0
numOdd = 0
numEvem = 0
FOR i = 1 TO 2n
IF i% 2 = 0 THEN
SumEven = SumEven + i
ELSE
SumOdd = SumOdd + Num
END IF
End For
PRINT SumOdd
PRINT SumEven
END

Question 6
Write down a computer solution that can be used to count the number of times a particular digit
appears in an (integer) input. Hint you may assume that there is an operator % that returns the
remainder when one integer is divided by another and has the syntax a %b where a and b are
integer constants.

Declare NumNum NoFind count Num NoNumfnd


INPUT NumNUm
INPUT Numfind
count = 0
NoNumfind = 0
WHILE (count < NumNUm)
INPUT Num
count = count + 1
IF (Num = Numfind) THEN
NoNumfind = NoNumfind + 1
END IF
WEND
PRINT NoNumfind; "is the Number of times"; Numfind, "appears in the digit"
END

Question 7
Assume that there is a function MID with the syntax, MID(s1,m,n) where s1 a
string, and m and n are integers. Given that MID(s1,m,n) is to return n middle
characters of s1 starting from the character at position m, write a computer solution that
accepts as input a string of characters and output the number of times a particular
character appears in the input.
Question 8
Write a computer solution that accepts as input an integer value and then output the sum of the
digits in the number as well as the number of digits it has with appropriate captions.
Input n as integer
Sum=0
Count=0
While (n/10>0)
Remainder=n%10
Sum=sum +remainder
Count=count+1
n=n/10
End while

Question 9
(a) A prime number may be defined as any number that has only two factors, the digit 1 and
the number itself. Using this definition, write down a computer solution that can
allow a number to entered as input and output a message indicating whether or not the
given number is a prime. Do not assume any pre-defined function.
Declare Num factor Numfactor
INPUT Num
NumFactor = 0
FOR factor = 1 TO Num
NEXT
IF Num MOD factor = 0 THEN
NumFactor = NumFactor + 1
END IF
IF NumFactor == 2 THEN
PRINT Num; "is a prime number"
ELSE
PRINT Num; "is not a prime number"
END IF
END

(b) Given a positive integer, N, devise a computer solution that can be used to find another
integer M whose factorial is N. For example if N is given as 720 then M should be 6.
(c)

Question 10
If p and q are both primes and q=p+2, then the pair p,q are called twin primes. For example, 5,7
are twin primes. Devise a computer solution to list all twin primes that are less than a positive
integer n.
Input N
Declare prime[N]

For i=1 to N step 1

For j=1 to I step 1

If i % j = 0

Factor = factor +1

End if

If factor == 2

For a = 0 to N

X[a] = i

End For

End if

End For

For a= 0 to N

x[a] = p

x[a+1] = q

if p = q +2

print p ‘and’ q “are twin primes.”

End if

End For

End

Question 11
Without using the division (/) devise a computer solution that can be used to m goes into n as well as
the remainder. The values for m and n will be read as inputs.
INPUT dividend
INPUT divisor
count = 0
For i=0 to dividend step divisor
Count=count +1
Remainder=devident-(count*divisor)
End for
PRINT count
PRINT remainder

END

Question 12
Brofoyedur is a small town in the Central Region of Ghana. It has been estimated that the
population of this town is about 2000 people and that the population is increasing at a rate of
15% every six months. You are required to write a computer solution to determine the number of
years that it would take for the population size to exceed two million for the village to become a
city.
Population=2000
Time =0
While (population<=2000000)
Time =time +0.5
Population=1.15*population
End while
Print time

Question 13
The square root of a number N can be approximated by a repeated calculation using the
following formula.
NewGuess = 0.5(LastGuess + N /LastGuess)

where NewGuess is the next guess and LastGuess the last guess. The calculation of a NewGuess
should be terminated when the absolute value of the difference between the NewGuess and the
LastGuess is about 0.0000001. Write down a computer solution for the above formula. You may
use the function ABS(x) or '|x|' to obtain the absolute value of x.
CLS
INPUT N
WHILE (AbsoluteX > 0.00000001)
INPUT newGuess
INPUT NextGuess
LastGuess=newGuess
newGuess=Nextguess
X = lastGuess - newGuess
AbsoluteX = ABS(X)
newGuess = 0.5 * (lastGuess + N / lastGuess)
End while
sqrtOfN = newGuess
End

Question 14
A number N may be said to be either a perfect, deficient or abundant number. If the sum of
divisors (excluding the number itself) equals the original number, the number is said to be
perfect. If the sum of the divisors is less than the number itself then the number is said to be
deficient otherwise the number is said to be abundant. For example, the number 12 has the
divisors 1, 2, 3, 4 and 6. The sum of these divisors is 16. Since the sum of the divisors of 6 is
greater than 6, we will say 6 is an abundant number. Write down a computer solution that can
classify a given number as a perfect, abundant or a deficient number with an appropriate caption.
INPUT N
sum = 0
FOR divisor = 1 TO N step 1
IF N % divisor = 0 THEN
sum = sum + divisor
END IF
IF sum == N THEN
PRINT N; "is a perfect number"
ELSE IF (sum < N) THEN
PRINT N; "is a deficient number"
ELSE
PRINT N; "is an abundant number"
END IF
END FOR
END

Question 15
The RUSSIAN PEASANT method is one way of finding the product of any two given numbers.
Given that A and B are two positive numbers, A is divided by 2 and its decimal part truncated,
while B is also multiplied by 2. This process of dividing and multiplying is repeated until A
attains a value of 1. The product of the two numbers is then the sum of all B values whose
corresponding A values ( including the initial value of A) are odd. As a typical example, let A and B
be 21 and 10 respectively then the division and the multiplication can be written down as follows:

Current value of A Current value of B


21 10
10 20
5 40
2 80
1 160

In the above, the bolded lines show when the value of A is odd. The product of the two numbers
will therefore be the sum of the bolded B values, which is 210 (10+40+160). Write a computer
solution that accepts as input two positive integers and then return the product of the two
numbers using the above method with an appropriate caption. Hint use the modulus operator (
%) where necessary.
sumB = 0
INPUT A
INPUT B
WHILE (A <= 1)
B=B*2
A=A/2
IF( A MOD 2! = 0 ) THEN
sumB=sumB+i
end for
end if
end while
end

Question 16
Assume you are the captain of JB and SONS Airline Ltd (JBSL) and you are approaching
Britain, a country that still measures distances in miles, yards and feet. Your range finder
unfortunately can read only distances in feet to the nearest whole number. Fortunately enough,
your plane has a small personal computer on which the C++ compiler has been installed. It has
become very necessary for you to write a simple computer solution that will later be coded in
C++ language to convert a number of distances in feet to miles, yards and feet. You should
accept as input a distance in feet and display the equivalent in miles, yards and feet. If a term is
zero, it should not be printed.

For example
5287 should be displayed as 1 mile 2 yards 1 foot
5279 should be displayed as 1759 yards 2 feet

Note: 1 mile = 1760 yards and 3 feet = 1 yard.


Declare
Input distance
Miles=distance/(1760*3)
Yards= b%(1760*3)/3
Feet=yards%miles
Print distance “is” miles “miles “, yards “yards and “feet “feet”

Question 17
Pay As You Study University of Ghana charges 1000 cedis for each semester hour of credit,
500000 cedis for regular room (R) and 1000000 cedis for an air-conditioned room (A) and
500000 cedis for use of academic facilities. All students pay 200000 for matriculation and
graduating students pay 300000 cedis for degree certificate. Devise a computer solution that
computes fees that must be paid by each student. A warning message should be displayed if a
student is taking more than 21 credit hours and less than 15 credit hours per semester. The input
per student should be students' id number (4 digits), room type (A or R), credit hours and
graduating status (Y or N).
Input ID
Input roomtype
Input graduating status
Do
Print”credit hour range is 15 and 25”
Input credit hours
While(credithour <15|| credithour>25)
hourCharges=1000*hours
if (roomR)
roomCharges=500000
else
roomCharges=1000000
end if
if(graduating)
Gcharges=200000+300000
Else Gcharges=200000
End if
End while
Fees=hourcharges+roomCharges+Gcharges
Print fees
End

Question 18
The transportation fare along a certain road in Ghana is calculated using the following sliding
scale:

• A fixed amount of ¢2000.00 is paid by all passengers


• The first 10 kilometers is free
The next 30 kilometers attract ¢100.00 each
The next 110 kilometers attract ¢175.00 each
Any excess kilometer over 150 kilometers attract ¢50.00.
Your problem is to write a computer solution that would allow a distance to be entered, compute
the required fare and then return as output the expected fare and the distance traveled.
Fare=2000
If (distance>10 &distance <=40)
Fare=fare+(distance -10)*100
Else if (distance >40 && distance <=150)
Fare=fare +30*100+(distance-40)*175
End if
End if
If(distance>150)
Fare=fare+(distance-150)*50
End if
Print distance ,fare
End

Question 19
Write a computer solution for evaluating the value of sin2x according to the formula
23 x 4 25 x 6 27 x 8
sin 2 x = x 2 − + − +
4! 6! 8!
Your computer solution should also return the number of terms needed to evaluate sin2x to 1 in
1,000,000 together with the value of ssssxxsin2x for a given x with appropriate captions. You
must put in a check so that your computer solution accepts x as input if only the absolute value of

x is less than .
2

Question 20
You are required to write a computer solution that can be used to display the numbers 0 to 99
inclusive in the following format, that is as a 10 by 10 array: Use writeln where necessary to
mean write on the next line.
0 10 20 30 40 50 60 70 80 90
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99

Declare Array [10][10]


For i=0 to 9 step 1
For j=0 to 9 step 1

Print( j-0)*10+1
print i+10*j
End for
End for
Question 21
A numeric palindrome is any number that can be read in both directions, that is from left to right
and from right to left to give the same value. For example, 13533531 is a numeric palindrome.
Write down a computer solution that can accept an n-digit integer number (not necessarily a
numeric palindrome) and find an n-digit number, M when appended to the original number
would make it a numeric palindrome.
Sum=0
Pos=1
Input n-digit num(a)
B= n-digit num
While (b>a)
Rem=b%10
Sum=sum +Rem *pos
Pos=pos *10
B=B/10
End while
M=sum
Print a Append Sum

Question 22
Write a computer solution to generate the Fibonacci series given the first two terms. For
example, if the first two terms of a series are 0 and 1, the next (3rd) term is obtained by adding
the two preceding terms (1st and 2nd), etc. Thus the third term of the series will be 1 and the
fourth will be 2, etc. Generally, the Fibonacci series is defined as follows:
f i = f i − 2 − f i −1 for i=3, 4, …., n
Declare f [n]
INPUT n
INPUT f [1]
INPUT f [2]
FOR i = 3 TO n STEP 1
f[i]= (f[i-2]) + (f[i-1])
PRINT f [i]
End for
Question 23
Employees of JB and SONS Consultants Limited are paid at an hourly rate of 25,000.00 cedis
per hour for regular hours and one and one-half times per hour for overtime hours in a week.
Any hour worked over forty hours per week is overtime. The following national tax sliding scale
is then applied to determine the amount of tax to be paid by an employee.

GROSS PAY TAX RATE (%)


Below 125,001 0
125,001 – 250,000 5
250,001 – 1,750,000 10
1,750,001 – 2,750,000 15
2,750,001 – 5,000,000 20
Over 5,000,000 30

In addition, 6% of an employee's gross pay is withheld for Social Security or GUSSS


contribution, 3% is withheld as constituency tax and 2% is withheld by the employer as welfare
contribution. If an employee has more than three dependents then an amount of 5,000.00 cedis is
paid for each dependent in excess of 3 towards National Health Insurance Scheme. The company
has no more than 100 employees. You are required to write a computer solution for computing a
worker's gross pay, the deductions and his/her net pay. Your computer solution should allow for
a number of staff details to be entered for the necessary computations.
WHILE (staff <= 100)
INPUT staffName
INPUT hours
INPUT NumDescendants

IF (hours <= 40) THEN


grosspay = 25000 * 40
ELSE
grosspay = (25000 * 40) + (40 - hours) * 35.500
END IF
WHILE (grosspay > 125001)
IF (grosspay <= 250000) THEN
tax = 0.05 * grosspay
ELSE IF (grosspay <= 175000) THEN
tax = 0.1 * grosspay
ELSE IF (grosspay <= 2750000) THEN
tax = 0.15 * grosspay
ELSE IF (grosspay <= 5000000) THEN
tax = 0.2 * grosspay
ELSE
tax = 0.3 * grosspay
END IF
sTax = 0.06 * grosspay
cTax = 0.03 * grosspay
eTax = 0.02 * grosspay
IF (NumDescendants > 3) THEN
NHIS = (NumDescendants - 3) * 5000
ELSE NHIS = 0
END IF
netPay = grosspay - (tax + sTax + cTax = eTax)
WEND

Question 24
Write down a computer solution for finding the standard deviation for a given set of X values. The
standard deviation,  is given by the following relation

 (x − xbar )
2
i
= i =1

n
where xi is a data point and xbar is the mean of the xis
declare X[n]
input n
sum=0
for i=1 to n step 1
print "Enter a number"
input X[i]
sum =sum + X[i]
end for
Xbar=sum/n
sum=0
for i=1to n
sum=sum(X[i]-xbar)*(X[i]-xbar)
end for
sd=sqrt(sum/n)

Question 25
Some three-digit numbers (in the range 100 to 999 inclusive) have the characteristic that the sum of
the cubes of the individual digits in a number equals to the number itself. A typical example of such
a number is 153 (13 + 53 + 33 = 1 + 125 + 27 = 153). You are required to write down a computer
solution for displaying all three digits numbers that have the above characteristics. Your computer
solution should be written such that no inputs are to be entered from the keyboard.

Question 26
A prime number is any positive integer that is divisible by itself and the number 1. Write down a
computer solution that can display as output all prime numbers less than or equal to 1000.
Factor =0
For i=1 t0 1000 step 1
For factor=1 to i step 1
If i%factor ==0
Factor = factor +1
End if
If factor==2
Print i as prime
End for
End for

Question 27
Given that a % b will give the remainder when a is divided by b, write a detailed computer
solution that can be used to convert a number in base 10 to a different base. The inputs to your
computer solution should be a number in base 10 and a base the number is to be converted to.
Input num
Input

Question 28
Write a computer solution that can be used to convert a number in a given base to base 10. The
input to your program should be the number to convert the base of number .
Declare X[1000]
Input num
Input base
For i=1 to X[num] step 1
Sum=sum +X[i]*base

Question 29
Write a computer solution that can be used to reverse the digits in a given integer and display the
new number. Hint, you are NOT to use a write statement within a loop structure
Sum=o
Pos=1
Input num
B=num
While(b>num)
Rem= num%10
Sum=sum+rem*pos
Pos=pos*10
B=b/10
End while
Print sum

Question 30
Assume that no programming language has a function for returning the result of one integer raised
to the power of another integer. It has become very necessary for you to write a computer solution
that can be used to calculate a b where a and b are integers. Note that a may also be a real number
but b should always be an integer.
Question 31
When one takes a loan, there are a number of repayment methods that one can adopt to pay the loan
n
 r 
Ar 1 + 
within a reasonable time. One of such methods is to use the formula RP =  100 
where PR
n
 r 
1 +  −1
 100 
is the monthly repayment amount, A is the amount borrowed, r is

the monthly interest rate and n is the number of months the loan is to be paid back. Write a
computer solution that can be used to calculate and display the monthly repayments until the loan is
fully paid back. Your input should be the amount borrowed, the annual interest rate and the number
of years the loan is to be paid back. The output should have the following layout

Month Repayment Balance Outstanding


1 xxxxxxx.xx xxxxxxxxxx.xx
2 : :
: : :
m xxxxx.xx 0

Question 32
Newton’s method is one way of finding the location of the zero or the root of a mathematical
function, f(x) say. The method works by initially guessing the x location of the zero. The
functional value at the current x is computed to get a better estimate of the zero. This process is
repeated until the difference between two successive estimates is at most 0.0000001. Each
estimate of xnew is computed from xold as follows:
f (x )
x new = xold − ' old
f ( xold )
Where f’(xold) is the derivative of the function f(xold). Given that f ( x) = 5 x 2 + 4 x − 72 write a
computer solution that will accept as input an initial guess value, xold and then find the zero of
the above function. Your output should be the root of the function and the number of iteration
performed to obtain the root.

Question 33
Talk and Talk More (TTM) mobile phone service provider requires a program to compute the
cost of national and international calls. National calls are of two types namely same network and
other network. The cost of the calls is determined as follows:
(i) Any call started between 7am and 7pm, Monday through Friday, is charged as
follows:
- 1200 and 2400 cedis per minute for same and other network respectively
- 8,200 cedis for international calls.
(ii) Any call started before 7am or after 7pm, Monday through Friday, is charged as
follows:
- 800 and 1400 cedis per minute for same and other network respectively
- 5,200 cedis for international calls.
(iii) Any call started on Saturday or Sunday is free, 600 and 4000 cedis respectively for
same network, different network and international calls.

The days of the week are coded 1 through 7 for Monday through Sunday respectively. Also the
same network, different network and international calls have codes 1, 2 and 3 respectively.

You are required to write a computer solution that will accept as input the code for the day and
the code for the type of the call, which is the same network, other network or international. In
addition, your computer solution should accept for each call the length of the call in minutes and
the time a call started. The time will be inputted in 24-hour notation so that time 1.48 is input as
1348.

Question 34
Assume that there is a function called tab which is used with an output statement such as the print
to specify the number of print positions to be left blank from the current cursor position. Let the
syntax of tab be tab(x) where x is the number of spaces required. You are required to write a
detailed computer solution for printing the calendar of a given input data. The input should be the
number of days in the month and the day (Monday, Tuesday, etc) on which the first of the month
falls. For example, if the number of days is 31 and the first of the month falls on a Wednesday then
your output should be as follows:

MON TUE WED THU FRI SAT SUN


1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

Question 35
There are a number of methods that are used for calculating the depreciation of an asset. One
commonly used method is the sum-of-the-years-digits. For example, consider equipment of value
15 million cedis required to be depreciated over 5 years. We first calculate the sum of the years'
digits by adding 1, 2, 3, 4 and 5. Thus the sum-of-the-years-digits is 15. For the first year the
5 4
amount of depreciation is x15million (i.e. 5million), the second year will be x15million (i.e.
15 15
3
4million), the third will be x15million (i.e. 3million) and so forth. You are required to devise a
15
computer solution to accept the value of an asset and the number of years of depreciation as inputs
and output the year, the amount of depreciation starting with year 1 up to the year required and the
balance outstanding at the end of each year under appropriate headings.
Enter Asset
Input year
originalYear=year
sum=year
while (year>=0)
year=year-1
sum =sum+year-1
for i=1 to originalYear step 1
dep =i/sum*asset
print “for “original year “depreciaction is” dep
end for

Question 36
Another method of calculating depreciation is the straight-line method. If value is the value of the
asset and it is to be depreciated over years years, then value/years is depreciated every year. Write a
detailed computer solution that accepts as input values for value and years and then display the
depreciation and the outstanding amount for the different years.
Input value
Input year
Sum=0
For i=1 to year step 1
Dep=value/year
Sum=sum+dep
Print “for “ I “years” sum”is the deprecitation”
End for

Question 37
In addition to the methods described in questions 36 and 37 for calculating depreciation, one can
also use the double-declining balance method. If the amount is the value of the asset and years
2
years is the number of years the asset is to be depreciated, then times the undepreciated
years
amount is depreciated every year. Since only a fraction of the undepreciated amount is depreciated,
the entire amount will never be depreciated fully. One way of solving this problem is therefore to
switch over to the straight-line method. You are required to devise a computer solution that
accepts as input amount, years and switch (the year in which to switch to the straight-line method)
and then display the depreciation and the outstanding amount for the different years.

Question 38 (a)
Devise a detailed computer solution to accept pairs of data points (x, y) as input and calculate the
equation of the line. Your output should be of the form y = ax + b , that is the line of best fit
where a and b are constants. The values for a and b can be obtained using the following
relations:
n n

 y i − b xi
a= i =1 i =1

n
n n

n  xi  y i
x y i i − i =1

n
i =1

b= i =1
2
n
 n 

i =1
x −   xi 
2
i
 i =1 
n

where
n

x y
i =1
i i = sum of the product x1y1 + x2y2 +…+ xnyn
n

x
i =1
i = sum of x-values=x1 + x2 + … xn
n

y
i =1
i = sum of y-values = y1 + y2 + … + yn
n

x
i =1
2
i = sum of squares of x-values = x12 + x22 + … +xn2

DECLARE x[1000] y[1000]


Input n
SumX=0
SumY=0
Sum XY=0
sumXsqrd
FOR i=1 to N step 1
Input X[i]
sumX= sumX+ sumX[i]
sumxsqd =sumXsqrd+(sumX[i]*sumX[i])
Input[Y]
sumY= sumY+ sumY[i]
sumXY=sumXY+ (X[i]*Y[i])

b=sumXY-[(sumX*sumY)/n]/[sumXsqd-(sumX*sumX)]
a=(sumY-b*(sumX))/n
END FOR

The values for a and b in question 38(a) can also be calculated as follows:
and

Question 39
Write a detailed computer solution that can be used to accept as input any amount in cedis and
display the number such that thousands are separated by commas. For example, an input of
1234567.78 should be displayed as 1,234,567.78.

Question 40
Some banks in the country use a very complicated way of granting loans. For example, if a
customer applies for a loan of 1000 cedis at an interest rate of 15% over a period of 18 months for
repayment, the interest is calculated as 1000 * 0.15 * 1.5 to give 225 cedis. The interest is
immediately deducted from the face value of 1000, leaving the customer with only 775 cedis.
Repayment is made in equal installments based on the face value. So, the monthly loan payment
will be 1000 divided by 18 months, which is 55.56 cedis. The method of calculation would have
been fair if the customer was actually looking for a loan of 775 but the customer receives 775
instead of 1000. Devise a computer solution based on the above method that will ensure that if a
customer wants a loan of 1000 cedis in his/her pocket he applies for an amount that will give him
exactly that. Thus, your computer solution should read as input the amount a user wants in his/her
pocket, the repayment period and the interest rate. The output should be the total loan and the
monthly installments.

Question 41
A newly established company, KKF Ltd sells electronic equipment to customers on a negotiable
credit plan. For example, if a piece of equipment sells at 1000 cedis at an interest rate of 18% (i.e
1.5% per month) and a monthly payment of 50 cedis, no down payment is required, however, the
monthly payment is used to pay the interest and whatever is left is used to pay part of the remaining
debt. Thus, for the first month, an amount of 1.5% of 1000 in interest (that is 15 cedis). The
remaining 35 (that is 50-15) cedis is used to pay part of the 1000 cedis which leaves the customer
with a debt of 965 cedis. For the next month, the interest will be 1.5% of 965, which is 14.48. Thus,
35.52 (50-14.48) cedis is deducted from the debt of 965 leaving the debt as 929.48. The process is
repeated until the full amount is paid. Please note that for the last month the balance plus the interest
may be less than 50 cedis yet the company will demand the full 50 cedis. Devise a computer
solution to read as input the value of the equipment, the interest rate, the period of repayment and
the monthly payment amount, and output the outstanding balances from the 1st month to the last
month.

Question 42
A young boy was sent by his mother to get some eggs from a nearby supermarket for his birthday
party. On his way home, he was knocked down by a motorist and had all the eggs broken. The boy
was unhurt and the motorist agreed to pay for the eggs. The boy could not remember the exact
number of eggs, only that when he took them out in pairs there was one left; similarly there was one
left when he took them out threes, fours, fives, or sixes at a time. When he took them out in sevens
at a time they came out even, that is there was nothing left. You are required to write a computer
solution to determine the smallest number of eggs the boy could have had and the total cost if the
boy claims that he paid 4 pesewas per egg.
Num=0
While(n%7==0)
Num=num+7
For I =2 to 6
If num% i==1
Print Num “as number of eggs”
Cost= Num*4
Print cost

End if
End for
End While.

Question 43
Modify your solution to question 42 such that all possible numbers of eggs that less than 500000
and their corresponding cost are displayed under appropriate headers.
While (num<500000)
Print cost=Num *4
Print Num
Print cost .

Question 44
One way of approximating the cube root of x given an initial approximation is to use Newton's
Method for computing the cube root,
1 x 
BetterAppoximation = OldApproximation +  2
− OldApproximation 
3  OldApproximation 

A continued BetterApproximation is calculated until the difference between successive


BetterApproximations is less than 0.000001. Devise a computer solution for finding the cube root
of a number using the above approximation.
Question 45
Without using any additional variable(s) to X and Y, device an algorithm that can be used to
interchange the content of two variables X and Y. Use inital values of X and Y to be 10 and 20
respectively to go through your solution and see if the new values of X and Y are now 20 and 10
respectively.
DECLARE a b c

X=X+Y

Y=X-Y

X=X-Y

You might also like