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

Programming Question answer

A proposed solution to a question

Uploaded by

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

Programming Question answer

A proposed solution to a question

Uploaded by

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

ALGORITHM Factorial (n)

SET fact  1
FOR I  1 TO n
fact  fact * i
END FOR
RETURN fact
END ALGORITHM

Work required:
a) Re-write the above algorithm using the while loop
b) What is the output of this loop if n = 7?
c) Draw the flowchart of this loop ?
d) What is the main difference between pre-test and post-test loop ? Give an example

Give Simple definitions of the following terms as used in algorithm:


a. Pseudocode
b. Variables
c. Control structure
d. Flowchart

Study the algorithm below:


ALGORITHM IsPrime(n)
IF n <= 1 THEN
RETURN False
ENDIF

FOR I  2 To n – 1 DO
IF n MOD I = 0 THEN
RETURN False
ENDIF
ENDFOR
RETURN True
END ALGORITHM

Work Required:
a) How does the algorithm handle negative numbers ?
b) What is the result if n = 2, the smallest prime number ?
c) How many iterations does the loop run for n = 7 ?
d) What happens if the input is an even number greater than 2 ?
e) Modify the pseudocode to return all prime numbers up to a given number N.
f) Implement the above pseudocode in c programing Language.

a) What are the four main pillars of OOP? Explain each


b) What is the difference between a class and an object ?
c) How does encapsulation contribute to data security in OOP ?
d) What is the difference between private and public class members
e) Write a program that defines a student class with the following attributes:
- name (String)
- age (int)
- grade (double)
Implement:
- A parameterized constructor to initialize the attributes.
- Getter and setter methods for each attribute.
- A method to display student details.
- Create an object and display the details.

You might also like