Write a code To check friendly pair

Q31. Write a code To check friendly pair

Program To Check Friendly Pair : This section focuses on Friendly Pair algorithm and program. The programs should be practiced to improve the coding skills required for various interviews (campus interviews, walk-in interviews), coding rounds etc.

Abundancy Pair : An Abundancy Pair is a number for which the sum of its proper divisors is greater than the number itself.
Friendly Pair : Two numbers are said to be friendly pairs if they have common abundancy index.
Given an number m and n and our task is to find Abundancy index of both the number and then need to check if they have common abundancy index or not.

For Example :-

Input : m= 28, n = 6
Output : Yes, The number is Friendly Pair
Explanation : As we can see that input number m = 28 and n = 6. The proper divisors of 6 is 1, 2, 3, 6 and proper divisors of 28 is 1, 2, 4, 7, 14, 28. The sum of proper divisors is 1 + 2 + 3 + 6 = 12 and 1 + 2 + 4 + 7 + 14 + 28 = 56. Abundancy index of 6 and 28 are 2. So they are friendly pair.

 Algorithm For Check Friendly Pair

START
Step 1 : Initilize numbers m and n.
Step 2 : Initialize two variables, sum1 and sum 2 with zero
Step 3 : Assign sum 1 with the sum of all the divisors of number m
Step 4 : Assign sum 2 with the sum of all the divisors of number n
Step 5 : If (sum 1==number1) and (sum 2==number 2), then print, “Friendly Numbers”
Step 6 : Else print “Not Friendly Numbers”
Stop

Code For Check Friendly Pair

Output

Yes, The number is Friendly Pair

Write a code to print pascal’s triangle.

Q30. Write a code to print Pascal's triangle

Here you will find an Algorithm And Program to print Pascal’s Triangle. Given an integer value n and our task is to print first n lines of the Pascal’s triangle
Pascal’s Triangle : Pascal’s triangle is a triangular array of binomial coefficients. Pascal’s triangle is a type of number pattern. The numbers are so arranged that they are reflected as triangles. For Example :-

Input : Enter the number of rows: 6

Output:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Explanation : As we can see that input integer number : Num = 6. Output will be the Pascal’s Triangle.
Now let us understand what logic we need to use in this – In the program three loop are used. :
1. Outer loop will run throughout the program. The loop will start from i = 0 to rows.
2. Inner loop 1 is used to give space between the number. The loop will start from space = 1 to rows-i
3. Inner loop 2 is used to calculate the coef and print the coef. The inner loop 2 start from j= 0 to i.

Algoritham For Print Pascal’s Triangle

START
Step 1: [ Take Input ] Read: Number of rows
Step 2 : [Define] coef = 1, space, i, j.
Step 3: Loop start from i = 0 to rows:
Loop start from space = 1 to rows-i[for space between number]:
print space ” “
Loop end
Loop start from j= 0 to i:
[Check If Condition] if i or j equals 0
print 1
[Check Else Condition]
print coef * (i – j + 1) / j;
Loop end
Give one line space
Loop end
Stop

Code For Print Pascal’s Triangle

Output

Number of rows: 6
Output :
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Left array rotation

Q29. Write a code for Left array rotation

Array Rotation:- Array rotation means moving array elements to the left or right of an array by specific position. An array can be moved to the number of given positions by rotating left (clockwise) or right (anti-clockwise). Here we will be discussing left array rotation.

Algorithm For Left array rotation

START

Stop

Code For Left array rotation

Output

9,11,13,15,2,5,7

 

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

sorting in ascending order using bubble sort

Q28.Write a code to sort array elements in ascending order using bubble sort

Bubble Sort :- Bubble sort is also known as sinking sort. Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if order is wrong.

Ascending Order :- Numbers are said to be in ascending order when they are arranged from smallest to largest number. Such as 2, 3, 5, 9 and 11 are arranged in ascending order.

Arrange Array elements in ascending order using Bubble Sort Algorithm 

START
Step 1: Repeat Step 2 For i = 0 to N
Step 2: Repeat For J = 0 to N – I
Step 3: IF A[J] > A[J+1]
SWAP A[J] and A[J+1]
[END OF INNER LOOP]
[END OF OUTER LOOP
Step 4: EXIT
STOP

Arrange Array elements in ascending order using Bubble Sort Code

Output

Sorted Array: 2 3 5 7 8

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

search element using binary search algorithm

Q27. Write a code to search element in array using binary search algorithm

Binary Search :- Binary search is a fast search algorithm with run-time complexity of search O(log n). For this algorithm to work properly, elements must be in a sorted form.

Binary Search Tree Algorithm

START
do until the pointers low and high meet each other.
mid = (low + high)/2
if (x == arr[mid])
return mid
else if (x > A[mid]) // x is on the right side
low = mid + 1
else // x is on the left side
high = mid – 1
STOP

Code Binary Search Tree

Output

Element is found at index 3

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number