jbmienwipia14@gmail.
com 03 Aug 2024
FOR UNILAK STUDENTS CIS JAN 2024
Fundamentals of Algorithm
Model Questions Answered
Q1) Algorithm to check if a positive integer is divisible by either 3 or 7
Algorithm
START
VAR num AS Integer
READ num
IF ((num%3 == 0)||(num%7 == 0)) THEN
WRITE "The number is divisible by either 3 or 7."
ELSE
WRITE "The number is not divisible by either 3 or 7."
END IF
STOP
Q2) C program to compute the sum of two integers and check if it's in
the range 10 to 20
C Program
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
sum = a + b;
if (sum >= 10 && sum <= 20) {
printf("The sum is within the range 10 to 20.\n");
} else {
printf("The sum is not within the range 10 to 20.\n");
}
return 0;
}
Pseudo Codes
START
VAR a, b, sum AS Integer
WRITE "Enter two integers: "
READ a, b
sum = a + b
Prepare by Mihigo ER Anaja, author of time and legacy
Find more books and learning resources from:
https://payhip.com/kinbook
jbmienwipia14@gmail.com 03 Aug 2024
FOR UNILAK STUDENTS CIS JAN 2024
IF ((sum >= 10)&&(sum <= 20) THEN
WRITE "The sum is within the range 10 to 20."
ELSE
WRITE "The sum is not within the range 10 to 20."
END IF
STOP
Q3) C program to check if y is greater than x and z is greater than y
C Program
#include <stdio.h>
int main() {
int x, y, z;
printf("Enter three integers: ");
scanf("%d %d %d", &x, &y, &z);
if ((y > x) && (z > y)) {
printf("True\n");
} else {
printf("False\n");
}
return 0;
}
Pseudo Code
START
VAR x, y, z AS Integer
WRITE "Enter three integers: "
READ x, y, z
IF ((y > x)&&(z > y)) THEN
WRITE "True"
ELSE
WRITE "False"
END IF
STOP
Q4) C program to compute the sum of the elements of an array of
integers
C Program
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int sum = 0;
int i;
Prepare by Mihigo ER Anaja, author of time and legacy
Find more books and learning resources from:
https://payhip.com/kinbook
jbmienwipia14@gmail.com 03 Aug 2024
FOR UNILAK STUDENTS CIS JAN 2024
for (i = 0; i < 5; i++) {
sum += arr[i];
}
printf("Sum of elements in the array: %d\n", sum);
return 0;
}
Pseudo Code
START
VAR arr AS Array[5] = [10, 20, 30, 40, 50]
VAR sum AS Integer = 0
VAR i AS Integer
FOR i=0 TO 4 DO
sum = sum + arr[i]
END FOR
WRITE "Sum of elements in the array: " + sum
STOP
Q5) C program to create a new array taking the first and last elements of
a given array of integers
C Program
#include <stdio.h>
int main() {
int originalArr[5] = {10, 20, 30, 40, 50};
int newArr[2];
int i;
printf("Elements in original array are: ");
for (i = 0; i < 5; i++) {
printf("%d, ", originalArr[i]);
}
printf("\n");
newArr[0] = originalArr[0];
newArr[1] = originalArr[4];
printf("Elements in new array are: %d, %d\n", newArr[0], newArr[1]);
return 0;
}
Pseudo code
START
VAR originalArr[5] AS Integer
VAR newArr[2] AS Integer
Prepare by Mihigo ER Anaja, author of time and legacy
Find more books and learning resources from:
https://payhip.com/kinbook
jbmienwipia14@gmail.com 03 Aug 2024
FOR UNILAK STUDENTS CIS JAN 2024
VAR i AS Integer
originalArr= [10, 20, 30, 40, 50]
WRITE "Elements in original array are: "
FOR i FROM 0 TO 4
WRITE originalArr[i] + ", "
END FOR
WRITE "\n"
newArr[0] = originalArr[0]
newArr[1] = originalArr[4]
WRITE "Elements in new array are: " + newArr[0] + ", " + newArr[1]
STOP
Q6) C program to count the even number of elements in a given array of
integers
C Program
#include <stdio.h>
int main() {
int arr[5] = {10, 23, 30, 41, 50};
int count = 0;
int i;
for (i = 0; i < 5; i++) {
if (arr[i] % 2 == 0) {
count++;
}
}
printf("Number of even elements in the array: %d\n", count);
return 0;
}
Pseudo code
START
Prepare by Mihigo ER Anaja, author of time and legacy
Find more books and learning resources from:
https://payhip.com/kinbook
jbmienwipia14@gmail.com 03 Aug 2024
FOR UNILAK STUDENTS CIS JAN 2024
VAR arr AS Integer
VAR count AS Integer
VAR i AS Integer
count=0
arr = [10, 23, 30, 41, 50]
FOR I=0 TO 4 DO
IF ((arr[i]%2) == 0) THEN
count = count + 1
END IF
END FOR
WRITE "Number of even elements in the array: " + count
STOP
Q7) Algorithm to determine the maximum between two numbers
Algorithm
START
VAR num1, num2 AS Integer
INPUT num1
INPUT num2
IF (num1 > num2) THEN
WRITE "The maximum number is: " + num1
ELSE IF (num2 > num1) THEN
WRITE "The maximum number is: " + num2
ELSE
WRITE "Both numbers are equal."
END IF
STOP
Q8) Algorithm to check if a given year is a leap year or not
Algorithm
START
VAR year AS Integer
INPUT year
Prepare by Mihigo ER Anaja, author of time and legacy
Find more books and learning resources from:
https://payhip.com/kinbook
jbmienwipia14@gmail.com 03 Aug 2024
FOR UNILAK STUDENTS CIS JAN 2024
IF (year%4 == 0) THEN
WRITE "The year is a leap year."
ELSE
WRITE "The year is not a leap year."
END IF
STOP
Q9) Algorithm to determine whether a letter is a vowel or consonant
Algorithm
START
VAR vowels[10], letter AS Character
VAR isVowel AS Boolean
VAR i AS Integer
vowels= ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
isVowel = FALSE
WRITE “Enter any letter”
READ letter
FOR i=0 to 9 DO
IF (letter == vowels[i]) THEN
isVowel = TRUE
BREAK
END IF
END FOR
IF (isVowel==true) THEN
WRITE "The letter is a vowel."
ELSE
WRITE "The letter is a consonant."
END IF
STOP
Q10) Algorithm to check if a given number is a perfect square or not
Algorithm
START
VAR num, sqrt AS Integer
Prepare by Mihigo ER Anaja, author of time and legacy
Find more books and learning resources from:
https://payhip.com/kinbook
jbmienwipia14@gmail.com 03 Aug 2024
FOR UNILAK STUDENTS CIS JAN 2024
WRITE “Insert a number”
READ num
sqrt = SQRT(num)
IF(( sqrt * sqrt) == num) THEN
WRITE "The number is a perfect square."
ELSE
WRITE "The number is not a perfect square."
END IF
STOP
Note: The SQRT function is assumed to be available in the algorithm. In a
real programming language, you would use a library function or
implement the square root calculation manually.
Q11) Algorithm program to swap two numbers
Algorithm
START
VAR num1, num2, temp AS Integer
WRITE “Insert 2 number”
READ num1, num2
WRITE "Inserted values: num1 = " + num1 + ", num2 = "+num2+"\n"
temp = num1
num1 = num2
num2 = temp
WRITE "Swapped values: num1 = " + num1 + ", num2 = " + num2
STOP
Flow Chart
Prepare by Mihigo ER Anaja, author of time and legacy
Find more books and learning resources from:
https://payhip.com/kinbook
jbmienwipia14@gmail.com 03 Aug 2024
FOR UNILAK STUDENTS CIS JAN 2024
Q12) Algorithm program to determine if a number is positive, negative,
or zero
Algorithm
START
VAR num AS Integer
INPUT num
IF (num > 0) THEN
WRITE "The number is positive."
ELSE IF (num < 0) THEN
WRITE "The number is negative."
ELSE
WRITE "The number is zero."
END IF
STOP
Prepare by Mihigo ER Anaja, author of time and legacy
Find more books and learning resources from:
https://payhip.com/kinbook
jbmienwipia14@gmail.com 03 Aug 2024
FOR UNILAK STUDENTS CIS JAN 2024
Q13) Algorithm program to count the number of digits in a number
Algorithm
START
VAR num,count AS Integer
READ num
count = 0
WHILE (num > 0) DO
num = num / 10
count = count + 1
END WHILE
WRITE "The number of digits is: " + count
STOP
Q14) Algorithm program to check which numbers in an array are even
Algorithm
START
VAR arr[5],i AS Integer
WRITE “Enter 5 Numbers\n”
FOR i = 0 TO 4 DO
READ arr[i]
END FOR
FOR i = 0 TO 4 DO
IF (arr[i]%2 == 0) THEN
WRITE "The number " + arr[i] + " is even."
END IF
END FOR
STOP
Q15) Algorithm program to count the number of words in a sentence
Algorithm
START
VAR sentence AS String
VAR count AS Integer
READ sentence
count = 1
FOR i = 0 TO LENGTH(sentence) - 1 DO
Prepare by Mihigo ER Anaja, author of time and legacy
Find more books and learning resources from:
https://payhip.com/kinbook
jbmienwipia14@gmail.com 03 Aug 2024
FOR UNILAK STUDENTS CIS JAN 2024
IF (sentence[i] == ' ') THEN
count = count + 1
END IF
END FOR
WRITE "The number of words is: " + count
STOP
Q16) Algorithm program to convert temperature from Celsius to
Fahrenheit
Algorithm
START
VAR celsius, fahrenheit AS Float
WRITE “Input the templature in celcius”
READ celsius
fahrenheit = (celsius *( 9 / 5)) + 32
WRITE "The temperature in Fahrenheit is: " + fahrenheit
STOP
Q17) Algorithm program to calculate the power of a number
Algorithm
START
VAR base, exponent AS Integer
VAR result AS Float
READ base
READ exponent
result = 1
FOR i = 0 TO exponent - 1 DO
result = result * base
END FOR
WRITE "The result of " + base + " raised to the power of "
WRITE exponent + " is: " + result
STOP
Note: In the algorithm for calculating the power of a number, I used a
simple iterative approach. In a real programming language, you would
use a more efficient method, such as the pow function.
Prepare by Mihigo ER Anaja, author of time and legacy
Find more books and learning resources from:
https://payhip.com/kinbook