Q1. Write pseudocode, using post condition loop, for printing all the Odd numbers between 100 and 200.
Understanding the question:
- The question requires a post-condition loop to be used - the REPEAT loop is a post-condition loop.
- Need the loop to print only odd numbers.
Solution 1:
Number ← 100
REPEAT
IF Number MOD 2 = 1 THEN
OUTPUT number
Number ← Number + 1
UNTIL Number = 200
Solution 2:
Number ← 101
REPEAT
OUTPUT number
Number ← Number + 2
UNTIL Number > 200
Question 2:
For a password to be valid, it must comply with the following rules:
- At least two lower-case alphabetic characters
- At least two upper-case alphabetic characters
- At least three numeric characters
- Alphanumeric characters only
A function, ValidatePassword, is needed to check that a given password follows these rules. This function takes a string, Pass, as a parameter and returns a boolean value:
TRUE if it is a valid password
FALSE otherwise
Write pseudocode to implement the function ValidatePassword.
SOLUTION:
FUNCTION ValidatePassword(STRING Pass) RETURNS BOOLEAN
|
||
DECLARE result: Boolean DECLARE Lcase: INTEGER DECLARE Ucase: INTEGER DECLARE Num: INTEGER DECLARE Letter: CHAR
Lcase ← 0 Ucase ← 0 Num ← 0 Result ← FALSE FOR x ← 1 TO LENGTH(Pass) |
||
Letter ← MID(Pass, x, 1) IF Letter >= ‘A’ AND Letter <= ‘Z’ THEN Ucase ← Ucase + 1 ELSE IF Letter >= ‘a’ AND Letter <= ‘z’ THEN Lcase ← Lcase + 1 ELSE IF Letter >= ‘0’ AND Letter <= ‘9’ THEN Num ← Num + 1 ELSE RETURN Result ENDIF |
||
NEXT x
|
||
IF Ucase >=2 AND Lcase >=2 AND Num >=3 THEN
|
||
Result ← TRUE RETURN Result |
||
ENDIF |
||
ENDFUNCTION
|
Details of the variables & Functions used:
Lcase - for storing the number of uppercase characters.
Lcase - for storing the number of lowercase characters.
Num - for storing the number of numerical characters.
Result - is set as FALSE initially, before validating the password.
The LENGTH function will return the number of characters in the password.
MID() Function returns a sub-string of the string passed into it. Here MID() function is used to extract characters one-by-one from the password.