1. Solve the given problems on arrays
1D ARRAY:
1. Program to reverse the elements of an array
Input:
arr = {1, 2, 3, 4, 5}
Output:
Reversed array : {5, 4, 3, 2, 1}
Algorithm to reverse an array
Input the number of elements of an array.
Input the array elements.
Traverse the array from the last.
Print all the elements.
Solution using C
#include<stdio.h>
int main()
{
int n,i, arr[20];
scanf(“%d”,&n);
for(i = 0; i < n; i++)
scanf(“%d”,&arr[i]);
printf(“Reversed array is:”);
for(i = n-1; i >= 0; i–)
printf(“%dt”,arr[i]);
return 0;
}
Solution using python3
n = int(input())
sum = 0
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
print("Reversed array is :",end=" ")
arr.reverse()
for i in range(0,n):
print(arr[i],end=" ")
Output:
3
3
2
1
Reversed array is : 1 2 3
2. Program to find the array type (even, odd or mixed array)
Sample Input:
5
2
4
1
3
5
Sample output:
Mixed
2. Algorithm to find the array type (even, odd or mixed array)
Input the number of elements of the array.
Input the array elements.
If all the elements of the array are odd, display "Odd".
If all the elements of the array are even, display "Even".
Else, display "Mixed".
Solution using C
#include<stdio.h>
int main()
{
int n,i, arr[20];
scanf(“%d”,&n);
int odd = 0, even = 0;
for(i = 0; i < n; i++)
scanf(“%d”,&arr[i]);
for(i = 0; i < n; i++)
{
if(arr[i] % 2 == 1)
odd++;
if(arr[i] % 2 == 0)
even++;
}
if(odd == n)
printf(“Odd”);
else if(even == n)
printf(“Even”);
else
printf(“Mixed”);
return 0;
}
Solution using python3
# Python program find the array type (even, odd or
mixed array)
n = int(input())
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
odd = 0
even = 0
for i in range(0,n):
if(arr[i] % 2 == 1):
odd = odd + 1
else:
even = even + 1
if(odd == n):
print(“Odd”)
elif(even == n):
print(“Even”)
else:
print(“Mixed”);
Output:
5
1 2 3 4 5
Mixed
3. Program to count the number of even and odd elements in an array
Sample Input:
3 (number of elements of the array)
1 (array elements)
2
3
Sample Output:
Odd: 2
Even: 1
3. Algorithm to count the number of even and odd elements in an array
Input the number of elements of the array.
Input the array elements.
Initialize count_odd = count_even = 0.
Traverse the array and increment count_odd if the array element is odd, else increment
count_even.
Print count_odd and count_even.
Solution using C
#include
int main()
{
int n,i, arr[20];
scanf(“%d”,&n);
for(i = 0; i < n; i++)
scanf(“%d”,&arr[i]);
int count_odd =0, count_even = 0;
for( i = 0; i < n; i++)
{
if(arr[i] % 2 == 1)
count_odd++;
else
count_even++;
}
printf(“Odd: %d”,count_odd);
printf(“nEven: %d”,count_even);
return 0;
}
Solution using python3
n = int(input())
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
count_odd = 0
count_even = 0
for i in range(0,n):
if(arr[i] % 2 == 1):
count_odd = count_odd + 1
else:
count_even = count_even + 1
print(“Odd:”, count_odd)
print(“Even:”, count_even)
Output:
5
1 2 3 4 5
Odd : 3
Even : 2
4. Program to check if two arrays are equal or not
Sample Input:
3
3
1
2
3
1
2
3
Sample output:
Same
4. Algorithm to check if two arrays are equal or not
Input the number of elements of arr1 and arr2.
Input the elements of arr1 and arr2.
Sort arr1 and arr2 using any sorting technique
If all the elements of arr1 and arr2 are not equal, then print "not Same".
Else, print " Same".
Solution using C
#include<stdio.h>
int sort(int arr[], int n)
{
int i,j;
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int arrays_equal(int arr1[], int arr2[], int n, int
m)
{
int i;
sort(arr1,n);
sort(arr2,m);
for(i = 0; i < n; i++)
{
if(arr1[i] != arr2[i])
{
return 0;
}
}
return 1;
}
int main()
{
int ,arr[20],n,m,i;
scanf(“%d”,&n);
scanf(“%d”,&m);
for(i = 0; i < n; i++)
scanf(“%d”,&arr1[i]);
for(i = 0; i < m; i++)
scanf(“%d”,&arr2[i]);
if(arrays_equal(arr1, arr2, n, m) == 0)
Solution using python3
def arrays_equal(arr1, arr2, n, m):
arr1.sort()
arr2.sort()
for i in range(0,n):
if(arr1[i] != arr2[i]):
return 0
return 1
n = int(input())
m = int(input())
arr1 = []
arr2 = []
for i in range(0,n):
temp = int(input())
arr1.append(temp)
for i in range(0,m):
temp = int(input())
arr2.append(temp)
if(arrays_equal(arr1, arr2, n, m) == 0):
print(“Not Same”)
else:
print(“Same”)
5. printf(“Not same”);
else
printf(“Same”);
return 0;
}
4. Program to find the sum of perfect square elements in an array
Input: arr = {1, 2, 3, 4, 5, 9}
The perfect squares are 1, 4, 9.
Sum = 1 + 4 + 9 = 14
Output: 14
Algorithm to find the sum of perfect square elements in an array
Input the number of elements of the array.
Input the array elements.
Initialize sum = 0.
Check if the array element is a perfect square.
If it is a perfect square, sum = sum + num.
Return sum
Solution using C
#include<stdio.h>
#include<math.h>
int isPerfectSquare(int number)
{
int iVar;
float fVar;
fVar=sqrt((double)number);
iVar=fVar;
if(iVar==fVar)
return number;
else
return 0;
}
int main()
{
int n,i,arr[20],sum=0;
scanf(“%d”,&n);
for(i = 0; i < n; i++)
scanf(“%d”,&arr[i]);
for(i = 0; i < n; i++)
sum = sum + isPerfectSquare(arr[i]);
printf(“%d”,sum);
return 0;
}
Solution using python3
from math import *
def isPerfectSquare(number):
if(sqrt(number) == floor(sqrt(number))):
return number
else:
return 0
n = int(input())
sum = 0
arr1 = []
for i in range(0,n):
temp = int(input())
arr1.append(temp)
for i in range(0,n):
sum = sum + isPerfectSquare(arr1[i])
print(sum)
6. Output:
4
1 4 9 16
30
5. Program to find the minimum scalar product of two vectors (dot product)
Sample Input 1:
3 (Number of elements of the array)
1 3 5 (Array 1 elements)
2 4 1 (Array 2 elements)
Sample Output 1:
15
Calculation:
Minimum scalar product = 1 * 4 + 3 * 2 + 5 * 1
= 4 + 6 + 5
= 15
Algorithm to find the minimum scalar product of two vectors
Input the number of elements of the arrays.
Input the array 1 and array 2 elements.
Initialize sum = 0.
Sort the array 1 in ascending order.
Sort the array 2 in descending order.
Repeat from i = 1 to n
o sum = sum + (arr1[i] * arr2[i])
Return sum.
Solution using C
#include<stdio.h>
int sort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
int sort_des(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] < arr[j+1])
Solution using python3
n = int(input())
arr1 = []
arr2 = []
for i in range(0,n):
temp = int(input())
arr1.append(temp)
for i in range(0,n):
temp = int(input())
arr2.append(temp)
arr1.sort()
arr2.sort(reverse=True)
sum = 0
for i in range(0, n):
sum = sum + (arr1[i] * arr2[i])
print(“Minimum Scalar Product :”,sum)
7. {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
int main()
{
int n ,i, arr1[n], arr2[n];;
scanf(“%d”,&n);
for(i = 0; i < n ; i++)
scanf(“%d”,&arr1[i]);
for(i = 0; i < n ; i++)
scanf(“%d”,&arr2[i]);
sort(arr1, n);
sort_des(arr2, n);
int sum = 0;
for(i = 0; i < n ; i++)
sum = sum + (arr1[i] * arr2[i]);
printf(“%d”,sum);
return 0;
}
6. Program to find the smallest and largest elements in an array
For example, consider the array.
arr = {1, 2, 3, 4, 5}
Smallest element : 1
Largest element : 5
Algorithm to find the smallest and largest numbers in an array
Input the array elements.
Initialize small = large = arr[0]
Repeat from i = 1 to n
if(arr[i] > large)
large = arr[i]
if(arr[i] < small)
small = arr[i]
Print small and large.
8. Solution using C
#include<stdio.h>
int main()
{
int a[50],i,n,large,small;
scanf(“%d”,&n);
for(i=0;i<n;++i)
scanf(“%d”,&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf(“nThe smallest element is
%dn”,small);
printf(“nThe largest element is
%dn”,large);
return 0;
}
Solution using python3
arr = []
n = int(input())
for i in range(0,n):
numbers = int(input())
arr.append(numbers)
print("Maximum element : ", max(arr),
"nMinimum element :" , min(arr))
Output:
5
1 2 3 4 5
The smallest element is 1
The largest element is 5
7. Program to print all the distinct elements in an array
SAMPLE INPUT:
9 = size of an array
2 3 4 5 6 1 2 3 4 = array elements
SAMPLE OUTPUT:
2 3 4 5 61
Algorithm to print distinct numbers in an array
Declare and input the array elements.
Traverse the array from the beginning.
Check if the current element is found in its previous elements the array again.
If it is found, then do not print that element.
Else, print that element and continue.
9. Solution using C
#include <stdio.h>
void distict_elements(int a[], int n);
int main()
{
int size_array, i, arr[20];
scanf(“%d”, &size_array);
for(i=0; i<size_array; i++)
scanf(“%d”, &arr[i]);
distict_elements(arr, size_array);
return 0;
}
void distict_elements(int a[], int n)
{
int i, j;
for (i=0; i<n; i++)
{
for (j=0; j<i; j++)
{
if (a[i] == a[j])
break;
}
if (i == j)
printf(“%dn “, a[i]);
}
}
Solution using python3
def distinct(arr, num):
arr1 = []
for i in range(0, num):
d = 0
for j in range(0, i):
if (arr[i] == arr[j]):
d = 1
break
if (d == 0):
arr1.append(arr[i])
print(“Distinct elements:”)
print (arr1)
n = int(input())
arr = []
for i in range(n):
arr.append(int(input()))
print(“Input array: “)
print (arr)
distinct(arr, n)
Output:
5
1 1 2 3 4
1
2
3
4
8. Program to check if the given arrays are disjoint
For example :
arr1 = {1,2,3,4,5}
arr2 = {6,7,8,9}
arr1 and arr2 elements are unique and hence they are said to be disjoint.
arr3 = {1,2,3,4,5}
arr4 = {4,5,6,7}
arr3 and arr4 are not disjoint as they have elements 4 and 5 in common.
Input:
10. 4
1 2 3 4
3
6 7 8
given arrays are disjoint
Algorithm to check if the given arrays are disjoint
Use two loops.
Traverse the array 1 using the outer loop.
Use the inner loop to check if the elements in array 2 are found in array 1.
If at least one element of array 2 is found in array 1, return false. Else return true.
Solution using C
#include <stdio.h>
int disjoint_arrays(int arr1[], int arr2[], int n,
int m)
{
int i,j;
for(i = 0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(arr1[i] == arr2[j])
return -1;
}
}
return 1;
}
int main()
{
int m,n,arr1[20],arr2[20],i,j;
printf("Enter the size of array 1n");
scanf("%d",&n);
printf("Input Array 1 elements n");
for(i=0;i<n;i++)
scanf("%d",&arr1[i]);
printf("Enter the size of array 2 n");
scanf("%d",&m);
printf("Input Array 2 elements n");
for(i=0;i<m;i++)
scanf("%d",&arr2[i]);
int res = disjoint_arrays(arr1,arr2,n,m);
if(res == -1)
printf("The arrays are not disjointn");
else
printf("The arrays are disjointn");
return 0;
}
Solution using python3
def disjoint_arrays(arr1,arr2):
for i in range(0,len(arr1)):
for j in range(0,len(arr2)):
if(arr1[i] == arr2[j]):
return -1
return 1;
arr1 = [1,2,3,4,5]
arr2 = [6,7,8,9,10]
res = disjoint_arrays(arr1,arr2)
if(res == -1):
print(“The arrays are not disjoint”)
else:
print(“The arrays are disjoint”)
11. 9. Program to find all symmetric pairs in an array. Two pairs (p,q) and (r,s) are said to be
symmetric when q is equal to r and p is equal to s. For example, (5,10) and (10,5) are symmetric
pairs
For example,
Consider a 2D array,
Input:
arr [6] [2] = {{1, 2}, {3, 4}, {5, 6}, {2, 1}, {4, 3},{10,11}}
Output:
{1,2} and {2,1} are symmetric
{3,4} abd {4,3} are symmetri
This problem can be solved in two different ways.
Method 1: Using two loops, one loop to traverse the pairs and the other loop to check if similar pair is
existing.
Method 2: An efficient way to solve this problem is to use hashing. Insert each array pairs into the hash
table with the first element of the pair as the key and the second element as the value. Traverse the hash
table to check if the pairs are found again.
Algorithm to find all symmetric pairs in an array
Input the array from the user.
1. Use two loops.
2. One loop for traversing the array and the other loop to check if symmetric pair is found in the
array.
3. If symmetric pair is found, print the pairs.
Algorithm to find symmetric pairs in an array using hashing
1. Input the array from the user.
2. From all the array pairs, the first element is used as the key and the second element is used as
the value.
3. Traverse all the pairs one by one.
4. For every pair, check if its second element is found in the hash table.
5. If yes, then compare the first element with the value of the matched entry of the hash table.
6. If the value and the first element match, it is displayed as a symmetric pair.
7. Else, insert the first element as the key and second element as value and repeat the same.
12. 10. Program to insert, delete and search an element in an array
Input format:
Input consists of 3 integers and 1 array.
Input the size of the array.
Input the array elements.
Input the position where the element should be inserted.
Input the element to be inserted in that position.
Sample Input:
5 (size of the array)
1 (array elements)
2
3
4
5
4 (Position)
10 (Element to be inserted)
Sample Output:
Array after insertion is:
1
2
3
10
4
5
Algorithm to insert, delete and search an element in an array
Insertion
Input the array elements, the position of the new element to be inserted and the new element.
Insert the new element at that position and shift the rest of the elements to right by one position.
Deletion
Input the array elements, the position of the new element to be inserted and the new element.
Delete the element and shift the rest of the elements to left by one position.
Search
Input the array elements, the element to be searched.
Traverse the array and check if the element is present in the array.
13. Display "Yes" if it is present in the array, else display "No".
Insertion:
Solution using C
#include <stdio.h>
int main()
{
int n,arr[20],i,pos;
scanf(“%d”,&n);
for(i = 0; i < n; i++)
scanf(“%d”,&arr[i]);
scanf(“%d”,&pos);
int ele;
scanf(“%d”,&ele);
if(pos > n)
printf(“Invalid Input”);
else
{
for (i = n – 1; i >= pos ; i–)
arr[i+1] = arr[i];
arr[pos] = ele;
printf(“Array after insertion
is:n”);
for (i = 0; i <= n; i++)
printf(“%dn”, arr[i]);
}
return 0;
}
Solution using python3
n = int(input())
sum = 0
arr = [1,2,3,4,5]
pos = int(input())
ele = int(input())
arr.insert(pos,ele)
for i in range(0,n):
print(arr[i], end = ” “)
Deletion:
Solution using C
#include <stdio.h>
int main()
{
int array[10], position, c, n;
printf(“Enter the number of elements of
the array : “);
scanf(“%d”, &n);
printf(“nInput the array elements : “);
for (c = 0; c < n; c++)
scanf(“%d”, &array[c]);
printf(“nEnter the position : “);
scanf(“%d”, &position);
if (position >= n+1)
printf(“nDeletion not possible.n”);
else
Solution using python3
arr = [1,2,3,4,5]
pos = int(input())
arr.remove(arr[pos])
for i in range(0,n-1):
print(arr[i], end = ” “)
14. {
for (c = position ; c < n – 1; c++)
array[c] = array[c+1];
printf(“nArray after deletion : “);
for (c = 0; c < n – 1; c++)
printf(“%dn”, array[c]);
}
return 0;
}
Searching:
Solution using C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int array[10], ele, c, n;
printf(“Enter the number of elements of
the array : “);
scanf(“%d”, &n);
printf(“nInput the array elements : “);
for (c = 0; c < n; c++)
scanf(“%d”, &array[c]);
printf(“nEnter element : “);
scanf(“%d”, &ele);
for(c = 0; c < n ; c++)
{
if(array[c] == ele)
{
printf(“nElement
foundn”);
exit(0);
}
}
printf(“element not foundn”);
return 0;
}
Solution using python3
n = int(input())
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
ele = int(input())
for i in range(0,n):
if(arr[i] == ele):
print("Element Found")
exit()
print("element not found")
15. 11. Program to sort the array of elements
Solution using C
#include<stdio.h>
int main()
{
int arr[20],n,i,j,temp;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter %d elements:n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Before sorting array elements
are:n");
for(i=0;i<n;i++)
printf("%dt",arr[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("nAfter sorting array elements
are:n");
for(i=0;i<n;i++)
printf("%dt",arr[i]);
return 0;
}
Solution using python3
n = int(input())
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
print("before sorting")
for i in range(0,n):
print(arr[i],end=" ")
print("nafter sorting")
arr.sort();
for i in range(0,n):
print(arr[i],end=" ")
OUTPUT:
Enter array size:5
Enter 5 elements:
3 4 2 5 1
Before sorting array elements are:
3 4 2 5 1
After sorting array elements are:
1 2 3 4 5
16. 2D ARRAY:
12. Program to find the maximum element in each row of a matrix
Input:
3 3 (Order of the matrix - number of rows and columns)
1 4 9
3 5 1
2 8 5
Output:
9
5
8
Algorithm:
Input the order of the matrix.
Input the matrix elements.
For row = 0 to n-1
Find the maximum element in the row
Print the maximum element
Solution using C
#include<stdio.h>
void maxi_row(int mat[][], int m, int n)
{
int i = 0, j,max;
for(i=0;i<n;i++)
{
max=0;
for ( j = 0; j < n; j++)
{
if (mat[i][j] > max)
max = mat[i][j];
}
printf("%dn",max);
}
}
int main()
{
int m, n, i, j, mat[20][20];;
scanf("%d %d",&m,&n);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf("%d",&mat[i][j]);
}
maxi_row(mat,m,n);
return 0;
}
Solution using python3
def maxi_row(arr):
no_of_rows = len(arr)
no_of_column = len(arr[0])
for i in range(no_of_rows):
max = 0
for j in range(no_of_column):
if arr[i][j] > max :
max = arr[i][j]
print(max)
mat = [[1,2,3],[4,5,6],[7,8,9]]
maxi_row(mat)
17. 13. Program to find the minimum element in each row of a matrix
Input:
3 3 (Order of the matrix - number of rows and columns)
1 4 9
3 5 1
2 8 5
Output:
1
1
2
Algorithm:
Input the order of the matrix.
Input the matrix elements.
For row = 0 to n-1
Find the minimum element in the row
Print the minimum element
Solution using C
#include<stdio.h>
#include<limits.h>
void min_row(int mat[][20], int m, int n)
{
int i = 0, j,min;
for(i=0;i<n;i++)
{
min = INT_MAX;
for ( j = 0; j < n; j++)
{
if (mat[i][j] < min)
min = mat[i][j];
}
printf("%dn",min);
}
}
int main()
{
int m, n, i, j, mat[20][20];;
scanf("%d %d",&m,&n);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf("%d",&mat[i][j]);
}
min_row(mat,m,n);
return 0;
}
Solution using python3
def maxi_row(arr):
no_of_rows = len(arr)
no_of_column = len(arr[0])
for i in range(no_of_rows):
min = 99999
for j in range(no_of_column):
if arr[i][j] < min :
min = arr[i][j]
print(min)
mat = [[1,2,3],[4,5,6],[7,8,9]]
maxi_row(mat)
18. 14.Program to find transpose of a matrix
Input:
3 3
1 2 3
4 5 6
7 8 9
Output:
1 4 7
2 5 8
3 6 9
Algorithm:
Input the order of the matrix.
Input the matrix elements.
For i = 0 to row_size
o For j= 0 to col_size
Transpose[j][i]=matrix[i][j]
Display the transpose matrix
Solution using C
#include <stdio.h>
int main()
{
int m, n, i, j, matrix[10][10], transpose[10][10];
scanf("%d%d", &m, &n);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &matrix[i][j]);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
transpose[j][i] = matrix[i][j];
printf("Transpose of the matrix:n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
printf("%dt", transpose[i][j]);
printf("n");
}
return 0;
}
Solution using python3
matrix=[]
row=int(input("Enter number of rows:"))
column=int(input("Enter number of columns:"))
for i in range(row):
matrix.append([])
for j in range(column):
num=int(input("Enter the element:"))
matrix[i].append(num)
print('n')
print("Input matrix:")
for i in range (row):
for j in range (column):
print (matrix[i][j], end=" ")
print('n')
transpose=[]
for j in range(column):
transpose.append([])
for i in range (row):
t_num=matrix[i][j]
transpose[j].append(t_num)
print('n')
print("Transpose matrix:")
for i in range (row):
for j in range (column):
print (transpose[i][j], end=" ")
print('n')
19. 15. Program to print the sum of boundary elements of a matrix
For example, consider the matrix given below.
1 2 3
4 5 6
7 8 9
Boundary Matrix
1 2 3
4 6
7 8 9
Sum of boundary elements: 1 + 2 + 3 + 4 + 6 + 7 + 8 + 9 = 40
Algorithm to print the sum of boundary elements of a matrix
Input the order of the matrix.
Input the matrix elements.
Print all the boundary elements of the matrix.
Find the sum of the boundary elements.
Print sum
Solution using C
#include <stdio.h>
#include<stdio.h>
int main()
{
int m, n, sum = 0;
printf(“nEnter the order of the matrix : “);
scanf(“%d %d”,&m,&n);
int i, j;
int mat[m][n];
printf(“nInput the matrix elementsn”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat[i][j]);
}
printf(“nBoundary Matrixn”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if (i == 0 || j == 0 || i == n – 1 || j == n – 1)
{
printf(“%d “, mat[i][j]);
sum = sum + mat[i][j];
}
else
printf(” “);
}
printf(“n”);
}
printf(“nSum of boundary is %d”, sum);
return 0;
}
Solution using python3
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sum = 0
m = 3
n = 3
print(“nBoundary Matrixn”)
for i in range (0, m):
for j in range (0, n):
if (i == 0 or j == 0 or i == n – 1 or j
== n – 1):
print(mat[i][j], end = ” “)
sum = sum + mat[i][j]
else:
print(end = ” “)
print(“n”)
print(“nSum of boundary is “, sum)
20. Patterns:
1. Program to print solid square star pattern
SAMPLE INPUT:
5
SAMPLE OUTPUT:
n x n matrix filled with „*‟
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Algorithm:
Input the number n
Repeat from i = 0 to n-1
o Repeat from j = 0 to n-1
Print *
Solution using C
#include <stdio.h>
int main()
{
int i, j,n;
scanf(“%d”,&n);
for(i = 0; i <n; i++)
{
for(j = 0; j < n; j++)
{
printf(“*”);
}
printf(“n”);
}
return 0;
}
Solution using python3
n = int(input())
for i in range(0,n):
for j in range(0,n):
print("*", end = " ")
print()
Output:
5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
21. 2. Program to print Hallow square star pattern
SAMPLE INPUT:
5
SAMPLE OUTPUT:
* * * * *
* *
* *
* *
* * * * *
Algorithm:
Input the number n
Repeat from i = 0 to n-1
o Repeat from j = 0 to n-1
If i = 0 or i=n-1 or j = 0 or j=n-1 then print *
Else print (“ “)
Print(“n”)
Solution using C
#include <stdio.h>
int main()
{
int i, j, n;
scanf(“%d”,&n);
for (i = 0; i <n; i++)
{
for (j = 0; j < n; j++)
{
if (i==0 || i==n-1 || j==0 || j==n-1)
printf(“*”);
else
printf(” “);
}
printf(“n”);
}
return 0;
}
Solution using python3
n = int(input())
for i in range(0, n):
for j in range(0, n):
if (i == 0 or i == n-1 or j == 0 or j == n-1):
print("*", end=" ")
else:
print(" ", end=" ")
print()
Output:
5
* * * * *
* *
* *
* *
* * * * *
22. 3. Program to print half pyramid pattern using stars
SAMPLE INPUT:
5
SAMPLE OUTPUT:
*
* *
* * *
* * * *
* * * * *
Algorithm:
Input the number n
Repeat from i = 0 to n-1
o Repeat from j = 0 to i
Print *
Print(“n”)
Solution using C
#include<stdio.h>
int main()
{
int i, j,n;
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
printf("* ");
}
printf("n");
}
return 0;
}
Solution using python3
n = int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print("*", end =" ")
print()
OUTPUT:
5
*
* *
* * *
* * * *
* * * * *
4. Program to print pyramid pattern printing using numbers
23. SAMPLE INPUT:
5
SAMPLE OUTPUT:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Algorithm:
Input the number n
Repeat from i = 0 to n-1
o Repeat from j = 0 to i
Print j
Print(“n”)
Solution using C
#include<stdio.h>
int main()
{
int i, j,n;
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
printf("%d ",j);
}
printf("n");
}
return 0;
}
Solution using python3
n = int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print(j, end =" ")
print()
OUTPUT:
5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5