Fundamentals of Algorithm Model Questions Answered
Fundamentals of Algorithm Model Questions Answered
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
Pseudo code
START
VAR originalArr[5] AS Integer
VAR newArr[2] AS Integer
VAR i AS Integer
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;
return 0;
}
Pseudo code
START
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
STOP
Q7) Algorithm to determine the maximum between two numbers
Algorithm
START
VAR num1, num2 AS Integer
INPUT num1
INPUT num2
STOP
Q8) Algorithm to check if a given year is a leap year or not
Algorithm
START
VAR year AS Integer
INPUT year
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
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
sqrt = SQRT(num)
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.
temp = num1
num1 = num2
num2 = temp
WRITE "Swapped values: num1 = " + num1 + ", num2 = " + num2
STOP
Flow Chart
STOP
count = 0
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
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.