LAB COMPONENT - 1
Algorithm, Flowchart & Pseudo code
Name
Regd. No.
Slot
Question No 1:
Find the nature and roots of the quadratic equation
Algorithm:
1. Start
2. Input the coefficients aaa, bbb, and ccc.
3. Compute the discriminant: Δ=b2−4ac\Delta = b^2 - 4acΔ=b2−4ac.
4. Check the nature of the roots:
a. If Δ>0\Delta > 0Δ>0, calculate two distinct real roots.
b. If Δ=0\Delta = 0Δ=0, calculate one repeated real root.
c. If Δ<0\Delta < 0Δ<0, calculate two complex roots.
5. Display the nature and values of the roots.
6. End.
Flowchart:
Pseudo code:
BEGIN
PRINT "Enter coefficients a, b, and c:"
INPUT a, b, c
COMPUTE discriminant: Δ = b^2 - 4*a*c
IF Δ > 0 THEN
root1 = (-b + sqrt(Δ)) / (2*a)
root2 = (-b - sqrt(Δ)) / (2*a)
PRINT "Roots are real and distinct:", root1, root2
ELSE IF Δ = 0 THEN
root1 = root2 = -b / (2*a)
PRINT "Roots are real and equal:", root1
ELSE
realPart = -b / (2*a)
imaginaryPart = sqrt(-Δ) / (2*a)
PRINT "Roots are complex:", realPart, "+", imaginaryPart, "i", "and", realPart, "-",
imaginaryPart, "i"
END
Question No 2:
Find Sum of N Numbers
Algorithm:
1. Start
2. Input the value of NNN (the total number of numbers).
3. Initialize sum = 0.
4. Repeat for i=1i = 1i=1 to NNN:
5. Input a number.
6. Add the number to sum.
7. Print sum.
8. End
Flowchart:
Pseudo code:
BEGIN
PRINT "Enter the number of elements (N):"
INPUT N
sum = 0 // Initialize sum to 0
FOR i = 1 TO N DO
PRINT "Enter number:"
INPUT num
sum = sum + num // Add the number to sum
ENDFOR
PRINT "Sum of", N, "numbers is:", sum
END
Question No 3:
Find Reverse a String
Algorithm:
1. Start
2. Take input string S
3. Reverse S using an appropriate method (loop or slicing)
4. Display the reversed string
5. Stop
Flowchart:
Pseudo code:
BEGIN
INPUT S
REVERSED_STRING ← ""
FOR i FROM LENGTH(S) DOWNTO 1 DO
REVERSED_STRING ← REVERSED_STRING + S[i]
END FOR
PRINT REVERSED_STRING
END
Question No: 4
Find palindrome number
Algorithm:
1. Start
2. Take input number N
3. Convert N to a string
4. Reverse the string
5. Compare the reversed string with the original
6. If they are the same, print "Palindrome"; otherwise, print "Not a palindrome"
7. Stop
Flowchart:
Pseudo code:
BEGIN
INPUT N
STRING_N ← CONVERT_TO_STRING(N)
REVERSED_N ← REVERSE(STRING_N)
IF STRING_N = REVERSED_N THEN
PRINT "Palindrome"
ELSE
PRINT "Not a palindrome"
ENDIF
END
Question No: 5
Find Leap Year or not.
Algorithm:
1. Start
2. Input the year (Y)
3. If Y is divisible by 400, then
4. Print "Leap Year"
5. Else if Y is divisible by 100, then
6. Print "Not a Leap Year"
7. Else if Y is divisible by 4, then
8. Print "Leap Year"
9. Else
10. Print "Not a Leap Year"
11. End
Flowchart:
Pseudo code:
BEGIN
INPUT Y
IF Y MOD 400 = 0 THEN
PRINT "Leap Year"
ELSE IF Y MOD 100 = 0 THEN
PRINT "Not a Leap Year"
ELSE IF Y MOD 4 = 0 THEN
PRINT "Leap Year"
ELSE
PRINT "Not a Leap Year"
ENDIF
END
Question No: 6
Swap Two Numbers with temporary variable.
Algorithm:
1. Start
2. Declare two variables, a and b, and a temporary variable temp.
3. Assign the value of a to temp.
4. Assign the value of b to a.
5. Assign the value of temp to b.
6. Print the swapped values of a and b.
7. End
Flowchart:
Pseudo code:
START
DECLARE a, b, temp
PRINT "Enter the value of a:"
INPUT a
PRINT "Enter the value of b:"
INPUT b
temp = a // Store the value of a in temp
a=b // Assign the value of b to a
b = temp // Assign the value of temp (original a) to b
PRINT "Swapped values are:"
PRINT "a = ", a
PRINT "b = ", b
END
Question No: 7
Count vowels in a string
Algorithm:
1. Take the input string.
2. Initialize a counter to 0.
3. Convert the string to lowercase for uniformity.
4. Loop through each character in the string:
5. If the character is a vowel (a, e, i, o, u), increment the counter.
6. Output the final count.
Flowchart:
Pseudo code:
FUNCTION CountVowels(S)
SET count = 0
SET vowels = "aeiou"
CONVERT S to lowercase
FOR each character C in S
IF C is in vowels
INCREMENT count
RETURN count
Question No: 8
Find the Anagrams of the given string
Algorithm:
1. Take the input string.
2. Generate all possible permutations of the string.
3. Store the unique permutations in a list or set.
4. Return the list of anagrams.
Flowchart:
Pseudo code:
FUNCTION FindAnagrams(S)
DEFINE result as an empty list
PERMUTE all possible arrangements of S
FOR each permutation P
IF P is not in result
ADD P to result
RETURN result
Question No: 9
Decimal to Binary Conversion
Algorithm:
1. Input: A decimal number n.
2. Check if n is 0: If yes, return "0".
3. Initialize an empty string for storing the binary result.
4. Repeat while n > 0:
a. Compute remainder = n % 2.
b. Append the remainder to the binary string.
c. Divide n by 2 (n = n // 2).
5. Reverse the binary string (since remainders are collected in reverse
order).
6. Output: The final binary string.
Flowchart:
#Paste the screenshot of the flowchart from Raptor/ online tool. It should clearly
visible# (Times New Roman – 12 pts)
Pseudo code:
def decimal_to_binary(n): if n == 0: return "0"
binary = ""
while n > 0:
binary += str(n % 2) #Get remainder (0 or 1)
n //= 2 #Divide by 2
return binary[::-1] # Reverse to get correct order
#Example Usage
num = 45
print(decimal_to_binary(num)) # Output: "101101"
Question No: 10
Bubble Sort
Algorithm:
Input: An array arr of size n.
Repeat n-1 times:
For each element (except the last sorted ones):
o Compare adjacent elements.
o Swap if they are in the wrong order.
If no swaps occur in a full pass, the array is sorted (optimization).
Output: The sorted array.
Flowchart:
#Paste the screenshot of the flowchart from Raptor/ online tool. It should clearly
visible# (Times New Roman – 12 pts)
Pseudo code:
def bubble_sort(arr):
n = len(arr)
for i in range(n - 1):
swapped = False
for j in range(n - i - 1):
if arr[j] > arr[j + 1]: # Swap if out of order
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
if not swapped: # Optimization: Stop if no swaps
break
# Example usage
nums = [5, 3, 8, 1, 2]
bubble_sort(nums)
print(nums) # Output: [1, 2, 3, 5, 8]