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

find pairs with given sum in an array

Q26. Write a code to find pairs with given sum in an array.

Solution : You are given an array and a number and task is to find all pairs of elements in an integer array whose sum is equal to a given number.

Array :- An array is a collection of data items, all of the same type, accessed using a common name.
For example: int arr[7]={5,8,7,5,6,5,4};

Algorithm  for find pairs of elements in an integer array

//Algorithm To Find All Pairs Of Elements In An Integer Array.
START
Step 1 -> Input ARRAY arr[] and NUMBER num.
Step 2 -> for i=0 to length of the arr[]-1:
for j=i+1 to length of the arr[]:
if arr[i]+arr[j] equals to num:
print the output
Loop End
Loop End
STOP

Code for find pairs of elements in an integer array

Output

4+5=9
7+2=9
6+3=9

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