print all distinct elements of a given integer array

Q19. Write a code to print all distinct elements of a given integer array

Distinct Element :- Distinct elements are nothing but the unique (non-duplicate) elements present in the given array.

Algorithm Distinct Elements In An Array

//Algorithm To Print All Distinct Elements Of A Given Integer Array.
START
Step 1 -> Take array as input arr.
Step 2 -> calculate size of array
Step 3 -> for loop i to n:
for loop j to i:
if arr[i] == arr[j]:
break;
if i == j:
print arr[i]
STOP

Code for Distinct Elements In An Array

Output

5,4,2,8,7,6

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

Write a code to add two matrices

Q18. Write a code to add two matrices

Solution :In this program, you’ll learn to add two matrices using Nested loop and display it using Nested loop.

Matrices:- A two dimensional(2D) array is known as Matrix. It can have m number of rows and n number of columns where m and n are two integer values.

Algorithm for Matrix Addition

//Algorithm to Add Two Matrices.
START
Step 1 -> Input matrix 1 and matrix 2.
Step 2 -> If the number of rows and number of columns of matrix 1 and matrix 2 are equal then execute step 3 else addition not possible
Step 3 -> for i=1 to rows[matrix 1]
for j=1 to columns [matrix 1]
Input matrix 1 [i,j]
Input matrix 2 [i,j]
matrix 3 [i,j]= matrix 1 [i,j] + matrix 2 [i,j];
step 4-> Display matrix 3 [i,j];
STOP

Code  for Matrix Addition

Output

mat1  mat2
1          2
3          4
5          6
7          8
sum matrix :
3        7
11     15

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

Write a code to find largest element in an array

Q17.Write a code to find largest element in an array.

Solution :- In this program, we will find the largest/Maximum value element in an array.

For Example :-
input : arr[]={ 2, 5, 3, 6, 4 }
output : 6

So as we can see that the largest element in arr[] = { 2, 5, 3, 6, 4 } is 6 so the output is 6.

Algorithm for Array Largest Number

START
Step 1 → Take an array Arr and define its values
Step 2 → Declare one variable (VAR) and assign 1st element of array to it
Step 3 → Loop for each value of Arr Repeat Step 4
Step 4 → If Arr[I] > VAR, Assign Arr[I] to VAR
Step 5 → After loop finishes, Display VAR which holds the largest element of array
STOP

Code for Array Largest Number

Output

Largest element in an Array is =6

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

 Write a code to find sum of array elements

Q16.Write a code to find sum of array elements

Array :- An array is a collection of data items, all of which are of the same type, using a common name.
For Example :-
input : arr[]={ 2, 5, 3, 6, 4 }
output : 20

So as we can see that the sum of elements in arr[] = { 2, 5, 3, 6, 4 } is 20 so the output is 20.

Algorithm for Array Sum

START
Step 1 : Take an array Arr and define its values
Step 2 : Loop for each value of Arr
Step 3 : Add each element to ‘sum’ variable
Step 4 : After the loop finishes, display ‘sum’
STOP

Code for Array Sum

Output

Sum of array is = 20

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