Вы находитесь на странице: 1из 4

Knockalva PolyTechnic College

Associate Degree - Computer Servicing and Electronics

Introduction to Programming (CPRG1201)

COURSE NOTES
(PRACTICE PROBLEMS)

Developing Pseudocode, Flowcharting and C# Skills

Consider the problem statements below. Study the proposed pseudocodes,


convert it to a flowchart and then implement the solution using C# code.
Make modifications where necessary.
1. Accept and arrange three integers in ascending order. (already done)

2. Classify a student’s score according to the following: (already done)


A+ =>90-100
A =>80-89
B =>65-79
C =>50-64
F => 0-49

3. Prompt a user for input and keep adding the number until a negative
number is entered. Display the sum when the negative number is
entered.

4. Check and display if a user entered number is a prime number. Prompt


user to try another number.

5. Check and display if an entered character is a vowel or consonant.

See other page for pseudocodes from 3 to 5.


3. Prompt a user for input and keep adding the number until a negative
number is entered. Display the sum when the negative number is entered.

To solve this problem, first we need to identify the variables that we need:
 We need a variable for the user input (say num)
 We need a variable to capture the sum (say sum)
Now we need to identify which type of control structure we need to use
(selection or repetition). Notice however that the question says we are to add
until a negative is entered. Thus we need to use a loop. In this case, we will
use the while loop.
NB: Generally, when we are using loops (or any control structure), we
initialize (set a starting value) the variables that will be used in the body
of the loop.
Now we are ready to write the pseudocode:
Declare num, sum as Integer
Initialize num, sum to zero
While num >= 0
Read num
sum = sum + num
Endwhile
PRINT sum
4. Check and display if a user entered number is a prime number. Prompt
user to try another number.
Again we need to determine the variables that we will need:
 We need a variable for the user input (say userNum)
 We need a variable to capture the user reply when they are prompted to
try another number (say option)
Typically when we want to prompt a user to try again, we use a do while loop.
Now, before we write our pseudocode, we need to determine how we would
check if a number is prime. Recall that a prime number has only two factors,
itself and one. Hence, if we can check that every between the number and one
is not a factor, we can conclude that the number is a prime number.

NB: When checking/manipulating numbers or data in an incremental


manner, or list, we typically use a for loop.
We will use a String variable isPrime as a prime check counter.

Now we are ready to write the pseudocode:


Declare userNum as Integer
Declare option, isPrime as String
Initialize option to “” and isPrime to “yes”
Do
Read userNum
For each integer i from 2 to (userNum – 1)
If the remainder of (userNum ÷ i) is equal to zero
isPrime = “no”
Stop loop
Endif
Endfor

If isPrime = “yes”
PRINT “Your number is a prime number”
Else
PRINT “Your number is not a prime number”
Endif

PRINT “Enter ‘again’ to try another number or any other key to exit”
Read option

While option = “again”


Endwhile
5. Check and display if an entered character is a vowel or consonant.
Again we need to determine the variables that we will need. This time we will
only one variable, userChar, to accept the user input.
In this situation, we trying check a single character against a specific list (in
this case the vowels). Hence, we use a selection control structure, namely the
switch (case).
The pseudocode:
Declare userChar as Character
Read userChar
Case of userChar
‘A’:
PRINT “You entered the vowel A”
‘E’:
PRINT “You entered the vowel E”
‘I’:
PRINT “You entered the vowel I”
‘O’:
PRINT “You entered the vowel O”
‘U’:
PRINT “You entered the vowel U”
default:
PRINT “You entered the consonant” + userChar
Endcase

Вам также может понравиться