230 likes | 310 Views
Learn how to check if a number is prime and generate the first 10 prime numbers through step-by-step algorithms. Also, explore patterns and pyramids based on user input. Merge ordered arrays and find Highest Common Factor (HCF) in three numbers.
E N D
Exercise 1 • Write an algorithm to check whether a number is a prime number or not.
Exercise 1: Solution • Accept a number from the user. Name it as num. • Declare an integer variable i. • Assign a value 2 to i. • Repeat until i > num/2: • If num % i = 0: • Display “The number is not prime” • Exit • i = i + 1 • 5. Display “The number is prime”.
Exercise 2 • Write an algorithm to generate the first 10 prime numbers.
Exercise 2: Solution • Declare an integer variable, count, and assign the value 0 to it. • Declare an integer variable, num, and assign the value 2 to it. • Repeat until count becomes equal to 10: • If num is a prime number: • Display num • Set count = count + 1 • Set num = num +1
Exercise 3 • Write an algorithm to accept a number between 1 and 9 and display a pattern. For example, if the number entered is 5, the following pattern should be displayed: • 1 • 2 1 • 3 2 1 • 4 3 2 1 • 5 4 3 2 1
Exercise 3: Solution • Accept a number in the range of 1 - 9 from the user. Store it in a variable, num. • Declare an integer variable, i. • Set i = 1. • Repeat until i becomes greater than num: // To display // num rows • Declare an integer variable j • Set j = i • Display j and insert a space • Set j = j – 1 • If j > 0 go to step c • Insert a line break • Set i = i + 1
Exercise 4 • Write an algorithm to accept a number between 1 and 9 and display a pyramid. For example, if the number entered is 5, the following pyramid will be displayed: • 1 • 1 2 1 • 1 2 3 2 1 • 1 2 3 4 3 2 1 • 1 2 3 4 5 4 3 2 1
Exercise 4: Solution • Accept a number from the user in the range of 1-9. Store it in a variable, n. • Set i = 1. • Repeat until i becomes greater than n: • Set j = 0 • Insert a space • Set j = j + 1 • If j < n – i, go to step b // To display n– i spaces • Set j = 1 • Display the value of j and insert a space • Set j = j + 1 • If j < i, then go to step f // To display numbers from 1 to i • Set j = i – 1
Exercise 4: Solution (Contd.) • Display the value of j and insert a space • Set j = j – 1 • If j > 0, go to step j // To display numbers from i – 1 down // to 1 • Give a line break • Set i = i + 1
Exercise 5 • Write an algorithm to accept two strings and check whether the second string exists within the first string. For example, if the first string is “concatenation” and the second string is “cat”, the algorithm should display “Substring found at position 4 in the string”. However, if the first string is “concatenation” and the second string is “tent”, the algorithm should display “Substring not found in the string”.
Exercise 5: Solution • Accept a string value from the user. Store it in a variable, str. • Accept the substring to be searched in str. Store it in a variable, substr. • Store the length of str in an integer variable, len1. • Store the length of substr in an integer variable, len2. • Set i = 0. • Repeat until i becomes equal to len1: • If str[i] != substr[0], go to step b else go to step d // To find the firstmatching character • Set i = i + 1 • If (i < len1) go to step a • If i = len1, go to step e, else go to step g • Display “Substring not found in the string” • Exit
Exercise 5: Solution (Contd.) • Set j = i // First character matched. Now match the // remaining characters • Set k = 0 • If str[j] = substr[k], go to step j, else go to step m • Set j = j + 1 • Set k = k + 1 • If j < len1 and k < len2, go to step i • If k = len2, go to step n, else go to step p • Display “Substring found at position ” + (i + 1) + “ in the string” • Exit • Set i = i + 1
Exercise 6 • Suppose you have two arrays of size 10 each containing elements in ascending order. Write an algorithm to merge the two arrays in such a way that the elements in the resulting array are arranged in the ascending order.
Exercise 6: Solution • Accept two arrays, A1 and A2, each of size 10, from the user. • Declare an array, result, of size 20. • Set i = j = k = 0. • If (A1 [i] <= A2 [j]): //Insert the smaller element in the//result array • result [k] = A1 [i] • Set k = k + 1 • Set i = i + 1 • If (A1[i] > A2[j]): //Insert the smaller element in the//result array • result [k] = A2 [j] • Set k = k + 1 • Set j = j + 1
Exercise 6: Solution (Contd.) • If (i < 10 and j < 10), go to step 4. //If none of the lists // has reached its end • Repeat until i equals 10: // If there are any elements left // in A1, copy them to result // array • result [k] = A1 [i] • Set k = k + 1 • Set i = i + 1 • Repeat until j equals 10: //If there are any elements left in // A2, copy them to the result // array • result [k] = A2 [j] • Set k = k + 1 • Set j = j + 1 • 9. Display result.
Exercise 7 • Write an algorithm to find the Highest Common Factor (HCF) of three numbers.
Exercise 7: Solution • Accept three numbers from the user. Store them in variables num1, num2, and num3. • Declare an integer variable, min. • Assign the value of the smallest number among num1, num2, and num3 to min by executing the following steps: • Set min = num1 • If (num2 < min), set min = num2 • If (num3 < min), set min = num3 • Declare an integer variable i. • Set i = min. • If ( num1 % i = 0 and num2 % i = 0 and num3 % i = 0 ): • Display i // If i divides all the numbers, then HCF is i • Exit
Exercise 7: Solution (Contd.) • Set i = i – 1. • Go to step 6.
Exercise 8 • Write an algorithm to multiply two 3 × 3 matrices.
Algorithm 8: Solution • Declare two 3 × 3 arrays, m1 and m2. • Accept the elements of the two matrices and store them in m1 and m2. • Declare a 3 × 3 matrix, result, to store the result of multiplication. • Set i = 0. • Set j = 0. • Set result [i, j] = 0. • Set k = 0. • result [i, j] + = m1 [i, k] × m2 [k, j]. • Set k = k + 1. • If k < 3, go to step 8. • Set j = j + 1.
Algorithm 8: Solution (Contd.) • If j < 3, go to step 6. • Set i = i + 1. • If i < 3, go to step 5. • Display result.
Algorithm 9 • Write a recursive algorithm to print the first n numbers in the Fibonacci series.
Algorithm 9: Solution • Algorithm: Fibo (n) • If n = 1, return 0 • If n = 2, return 1 • Return (Fibo (n – 1) + Fibo (n – 2))