Skip to main content

Array and Two-D Array In Java Programming

Introduction

An Array is a collection of constants of similar data type which are reference by a common name i.e. the array name.

Array elements store at contiguous (together in sequence) memory location. The lowest address corresponds to the first element and the highest address to the last element. Array can be of any data type but it always stores elements of its own type, i.e. array of integer type will store integers only not any other type values.

Need of Array

We need array to store multiple values of similar type for which multiple declaration of variable is not possible.
Like if we want to store marks of 50 students then it is not possible to declare 50 variables for them but we will simply use an array of integer to store these marks.

Indexing in Array

Array index always starts from 0. Array first element stores at 0th index and last element’s position will be one less than the total size of array.

Subscript

Subscript is the index of array. We can reading it by using a script variable. Subscript or Index of array always starts from 0 and ends with size-1 of array.

Sub Scripted Variable

Sub Scripted Variable is the variable that we use to visit elements of array. It can be any valid  identifier of Integer type.

Types of Array

Array are of two types.
1. Single Dimension Array
2. Multi Dimensional Array or Two Dimensional Array (Two-D Array) or Matrix

Single Dimension Array

An array with single row of elements is known as single dimension array. It represents its elements as a list and there is no tabular representation of elements.

Two Dimensional Array or Matrix (2D Array)

A 2-D Array or Double Dimension Array (Matrix) is a collection of elements placed in M Rows and N Columns. The syntax is used to declare a 2-D Array includes two subscripts, out of which one specifies the number of Rows and the other specifies the number of Columns of an array. These two subscripts are used to reference an element in an array.

So we can say that an array with two subscripts is known as Two D Array of Matrix. A Two D Array have single dimension arrays as it's elements.

For example Array [3][4] is a 2-D array containing 3 Rows (i.e. 3 Single dimension array) and 4 Columns and array[0][2] is an element placed at 0th Raw and 2nd Column in the array. 

Declaration of Single Dimension Array

Data type  Name_of_ array[] = new Data type [Size or Dimension]

Example:
int Marks[] = new int[10];

Initialization of Single Dimension Array

Data type  Name_of_Array [] = {value1,  value2, value3, value4, value5};

Example:
int Marks[]={10,20,30,40,50}; 


Program To Initialize and Print An Array

class ArrayInitialization
{
public static void main(String args[])
{
int array[]={1,2,3,4,5,6};
System.out.println("Array Elements Are ");
for(int i=0;i<array.length;i++)
{
System.out.println(array[i]);
}
}
}

Output


Video 

Program To Input And Print An Array 

import java.util.*;
class Array_Input_Print
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

//Declaration of an Integer Array of size 5
int Array[]=new int[5];

//Inserting Elements Into Array
System.out.println("Enter Elements of Array ");
for(int i=0;i<Array.length;i++)
{
Array[i]=sc.nextInt();
}

//Printing Elements From Array
System.out.println("Array Elements Are.....");
for(int i=0;i<Array.length;i++)
{
System.out.println(Array[i]);
}
}
}

Output


Printing Array By For Each Loop

class ForEachLoop
{
public static void main(String args[])
{
int Array[]={1,2,3,4,5};
//Printing Array Elements By For Each Loop
System.out.println("Array Elements By For Each loop ");
for(int Value:Array)
{
System.out.println(Value);
}
}
}

Output



Searching

Searching is the process by which we can search an element from an array. The search is said to be successful or unsuccessful depending on whether the element that is to be searched is found or not. Searching are of two types.
  • Linear Search
  • Binary Search

Linear Search

This is the simplest method of searching. In this method the element to be found is sequentially searched in the array. This method can be applied to a sorted or unsorted array. 

Searching is case of a sorted array starts form 0th element and continues until the element is found or an element whose value is greater than the value being searched is reached. 

But searching in unsorted array starts from the 0th element and continues until the element is found or the end of array is reached.

Linear search is less used these days because it is slower than binary search.

Program
import java.util.*;
class LinearSearch
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

int Array[]=new int[5];

System.out.println("Enter Element of Array ");
for(int i=0;i<Array.length;i++)
{
Array[i]=sc.nextInt();
}

boolean Found=false;

System.out.println("Enter Number That Is To Search ");
int number=sc.nextInt();
int position=-1;

//Searching Element
for(int i=0;i<Array.length;i++)
{
if(number==Array[i])
{
Found=true;
position=i;
break;
}   
}

if(Found==true)
{
System.out.println("Number Found At Position "+ (++position));
}
else
{
System.out.println("Number Not Found ");
}
}
}

Output


Video

Binary Search

This method is fast as compared to the linear searching technique. This method requires that the list of elements must be in sorted order.

In this method to search an element we compare it with the element present at the center of the array. 

If a matches is found then search is successful otherwise the array is divided into two halves (half). 

One from 0th element to the center element (first half), and second from next of center element to the last element (second half). 

As a result all the elements in first half are smaller than the center element, whereas all the elements in second half are greater than the center element. 

The searching now will proceed in either of the two halves (half) depending upon whether the element is greater or smaller than the center element. 

If the element is smaller then the center element than the searching will be done in the first half, otherwise in the second half. 

Same procedure is repeated till the element is found or the division of half gives one element.

Program
import java.util.*;
class BinarySearch
{     
public static void main(String args[])
{     
Scanner sc = new Scanner(System.in);
int Array[]=new int[5];
System.out.println("Enter Element of Array in Sorted Order ");
for(int i=0;i<Array.length;i++)
{
Array[i]=sc.nextInt();
}
System.out.println("Enter Number That Is To Search ");
int Number=sc.nextInt();
int lower=0, upper=Array.length-1, mid=0;
boolean found=false;
while(lower<=upper)
{  
mid=(lower+upper)/2;
if(Number==Array[mid])
{
found=true;
break;
}
else if(Number>Array[mid])
{
lower=mid+1;
}
else if(Number<Array[mid])
{
upper=mid-1;
}
}
if(found==true)
{
System.out.println("Number Is Found At Position " +(mid+1));
}
else
{
System.out.println("Number Not Found ");
}
}
}

Output


Video

Difference Between Linear Search And Binary Search

Linear Search
Binary Search
1. In this method the element to be found is            sequentially searched in the list. 1. This technique works on divide and search         rule.
2. This method can be applied to a sorted or           unsorted array. 2. This method is applicable for only sorted             array.
3. Searching starts from 0th element and                continues until the element is found 3. Searching starts from middle element and          continues until the element is found.
4. This method is slow as compared to the              binary searching technique, because one by        one element comparison involve in this              sorting. 4. This method is fast as compared to the linear      searching technique, because works on              divide and search rule.

Insertion of An Element Into Array At Given Position

import java.util.*;
class Insertion_Element
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

int Array[]=new int[1000];

System.out.println("Enter The Size of Array ");
int size=sc.nextInt();

if(size<1 || size>1000)
{
System.out.println("Size Should Be Greater Than 0 and Less Than Equals To 1000");
}
else
{

System.out.println("Enter Elements of Array ");
for(int i=0;i<size;i++)
{
Array[i]= sc.nextInt();
}

System.out.println("\n Enter The Position Where You Want To Insert New Element ");
int Position=sc.nextInt();

if(Position>=0 && Position<size)
{
boolean found=false;

for(int i=0;i<Array.length;i++)
{
if(i==Position)
{
found=true;
break;
}
}

if(found==true)
{
System.out.println("Enter Number To Insert ");
int Number=sc.nextInt();

for(int i=size;i>Position;i--)
{
Array[i]=Array[i-1];
}

Array[Position]=Number;
size++;

System.out.println("Array Elements After Insertion of New Element ");
for(int j=0;j<size;j++)
{
System.out.println(Array[j]);
}
}
else
{
System.out.println("Position Not Found");
}
}
else
{
System.out.println("Position Should Be Less Than Size of Array");
}
}
}
}

Output




Video


Deletion of Element From Array

import java.util.*;
class DeletionElement
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter Size of Array ");
int size=sc.nextInt();

int Array[]=new int[size];

System.out.println("Enter Array Elements ");
for(int i=0;i<size;i++)
{
Array[i]=sc.nextInt();
}

System.out.println("Enter Element That Is To Delete ");
int Number=sc.nextInt();

int position=0;
boolean found=false;

for(int i=0; i<size; i++)
{
if(Number==Array[i])
{
found=true;
position=i;
break;
}
}
if(found==true)
{
for(int i=position; i<size-1;i++)
{
Array[i]=Array[i+1];
}
size--;

System.out.println("Array Elements After Deletion Are ");
for(int i=0;i<size;i++)
{
System.out.println(Array[i]);
}
}
else
{
System.out.println("Element Not Found ");
}
}
}

Output



Video


Sorting

Sorting means arranging a set of data in some order. There are different methods that are used to sort the data in ascending or descending order. These methods can be divided into two categories.

Internal Sorting
If all the data that is to sort can be placed at a time in memory then internal sorting methods are used.
Example 
  • Selection Sort
  • Bubble Sort
  • Insertion Sort
  • Quick Sort
  • Merge Sort
  • Heap Sort
  • Binary Tree Sort (BT-Sort)
External Sorting
When data that is to sort is so large that some of the data is present in the memory and some is kept in auxiliary memory (hard disk, floppy, tape, etc.), then external sorting methods are used.

In this technique data from the disk is loaded into memory part by part and each part that is loaded is sorted and the sorted data is stored into some intermediate file. Finally all the sorted parts present in different intermediate files are merged into one single file.

Selection Sort

This is the simplest method of sorting. In this method, to sort the data in ascending order, the 0th element is compared with all other elements. 

If the 0th elements found to be greater than the compared element, then they are interchanged. So after the first iteration the smallest element is placed at the 0th position. 

Same procedure is repeated for all elements.

Program
import java.util.*;
class SelectionSort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
//Declaration of Array
int Array[]=new int[5];
//Insertion of Array Elements 
System.out.println("Enter Array Elements ");
for(int i=0; i<Array.length; i++)
{
Array[i]= sc.nextInt();
}
//Sorting Process
for(int i=0; i<Array.length-1; i++)
{
for(int j=i+1; j<Array.length; j++)
{
if(Array[i]>Array[j])
{
int t=Array[i];
Array[i]=Array[j];
Array[j]=t;
}
}
}
//Printing Sorted Array
System.out.println("Array Elements After Sorting Are ");
for(int i=0; i<Array.length; i++)
{
System.out.println(Array[i]);
}
}

}

Output




Video



Bubble Sort

In this method to arrange elements in ascending order 0th element is compared with the 1st element. If 0th element found to be greater than 1st element, then they are interchanged. 

Then the first element is compared with the second element, if it is found to be greater, then they are interchanged. 

In the same way all the elements are compared with their next element and are interchange if required. 

On completing this iteration the largest element gets placed at the last position. 

Similarly in the second iteration the comparisons are made till second last element and this time the second large element gets placed at the second last position in the list. 

As a result after all the iterations the list becomes a sorted list.

Program
import java.util.*;
class BubbleSort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
//Array Declaration
int Array[]=new int[5];
//Array Element Insertion
System.out.println("Enter Array Elements ");
for(int i=0; i<Array.length; i++)
{
Array[i]=sc.nextInt();
}
//Sorting of Array Element
for(int i=0; i<Array.length; i++)
{
for(int j=0; j<(Array.length-i-1); j++)
{
if(Array[j]>Array[j+1])
{
int tempvariable=Array[j];
Array[j]=Array[j+1];
Array[j+1]=tempvariable;
}
}
}
//Printing Array Elements After Sorting
System.out.println("Array Elements After Sorting Are ");
for(int i=0; i<Array.length; i++)
{
System.out.println(Array[i]);
}
}
}

Output



Video


Insertion Sort

Insertion sort is implemented by inserting a particular element at the appropriate position. In this method, the first iteration starts with comparison of 1st element with the 0th element. In the second iteration 2nd element is compared with the 0th and 1st element. In general, in every iteration an element is compared with all elements before it. 

During comparison if it is found that the element in question and can be inserted at a suitable position then space is created for it by shifting the other elements and then insert element at suitable position. 

This procedure is repeated for all the elements in the array.

Program
import java.util.*;
class InsertionSort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
//Array Declaration
int Array[]=new int[5];
//Inserting Element In Array
System.out.println("Enter Elements of Array ");
for(int i=0; i<Array.length; i++)
{
Array[i]=sc.nextInt();
}
//Insertion Sort
for(int i=1; i<Array.length; i++)
{
int key = Array[i];
int position=i;
while(position>0 && Array[position-1]>key)
{
Array[position]=Array[position-1];
position--;
}
Array[position]=key;
}
//Printing of Sorted Array
System.out.println("The Sorted Array Is ");
for(int i=0; i<Array.length; i++)
{
System.out.println(Array[i]);
}
}

}

Output




Video


Frequency Count of Each Element of Array 

Frequency is the number of occurrence of an element in the array. Frequency count means, counting number of occurrence of every element in an array.

In this process we run two loops, one loop will fetch element from array and second loop will count frequency of fetched element in the array. 

We have to keep track of processes elements to avoid duplicate printing.

Program
import java.util.*;
class Frequency_Count
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
//Array Declaration
int Array[]=new int[5];
//Insertion of Array Element
System.out.println("Enter Array Elements ");
for(int i=0; i<Array.length; i++)
{
Array[i]=sc.nextInt();
}
int Found, Counter;
//Frequency Count
for(int i=0; i<Array.length; i++)
{
Found=0;
Counter=1;
for(int j=0; j<Array.length; j++)
{
if(j>=i)
{
if(Array[i]==Array[j] && i!=j)
{
Counter++;
}
}
else if(Array[i]==Array[j])
{
Found=1;
break;
}
}
if(Found!=1)
{
System.out.println("Occurrence of "+Array[i]+" is "+Counter);
}
}
}

}

Output




Video



Removing Duplicate Elements From Array

We can remove duplicate elements from array by using two method. One by using a temporary array and second by using separate index. In this program we will use first method of temporary array.

In this program we will fetch every element of array and will count it's frequency. If frequency is 1 we will store this element into temporary array otherwise will skip the element.

After finish off this process will print the temporary array which contain unique elements of array.

Program
import java.util.*;
class Removing_Duplicate
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

//Array Declaration
int Array[]=new int[10];
int newArray[]=new int[10];

int counter=0;
int position=0;

//Inserting Array Elements
System.out.println("Enter Elements of Array");
for(int i=0;i<10;++i)
{
Array[i]=sc.nextInt();
}

//Removing_Duplicate
for(int i=0;i<Array.length;++i)
{
for(int j=i+1;j<Array.length;++j)
{
if(Array[i]==Array[j])
{
counter++;
}
}
if(counter==0)
{
newArray[position++]=Array[i];
counter=0;
}
else
{
counter=0;
}
}

//Printing Array with Unique Elements
System.out.println("Array After Removing Duplicate Elements ");
for(int i=0;i<position;++i)
{
System.out.println(newArray[i]);
}
}

}

Output




Copy Array

class Copy_Array
{
public static void main(String args[])
{
//Declaration of Array
int Array[]={10,20,30,40,50};
int temporaryArray[]=new int[10];
//Copying Array By Simple Assignment 
temporaryArray = Array;
//Printing Copied Array
System.out.println("Copied Array Elements Are ");
for(int i=0;i<temporaryArray.length;++i)
{
System.out.println(temporaryArray[i]);
}
}
}

Output



Copy Array by Java Function

System.copyarray() is a function of java.lang.System class.This method copies the source array from specified beginning position to the destination array from the mentioned position. Number of elements to be copied are decided by last argument provided in the function. 

This function takes 5 arguments. Description of those arguments is like this:
Syntax
System.arraycopy(Source Array, Source Position, Destination Array, Destination Position, Number of Elements To Copy);

Example
System.arraycopy(name, 0, firstName, 0, 8);

name        :  Source Array to be copied from.

0th index :  It is starting position of name (source) Array from where to copy.

firstName:  It is Destination Array to be copied in.

0th index :  It is starting position of firstName (Destination) Array, where to copy in.

8               :  It is total number of elements to be copied.
Program
class CopyStringArray
{
public static void main(String args[])
{
//Array Declaration
char name[]={'m','o','h','a','m','m','a','d',' ','a','m','i','r'};
char firstName[]=new char[20];
//Copying Array by arraycopy() function 
System.arraycopy(name,0, firstName,0,8);
//Prining Copied String Array
System.out.println(new String (firstName));
}
}

Output




Copy Two Array Into One

By the help of System.copyarray() function (discussed above) we can copy multiple arrays into one. For this the we have to declare destination array with size equal to the total of lengths of all arrays which we have to copy.

Program
class CopyTwoArray
{
public static void main(String args[])
{
//Declaration and initialization of Array
int Array1[]={12,2,4,53,53,67};
int Array2[]={3,5,11,45};
int Array3[]=new int[Array1.length+Array2.length];
//Copying First Array Into Third Array
System.arraycopy(Array1,0,Array3,0,6);
//Copying Second Array Into Third Array
System.arraycopy(Array2,0,Array3,6,4);
//Array Printing
System.out.print("\nFirst Array\t: ");
for(int i=0; i<Array1.length; i++)
{
System.out.print(Array1[i]+" ");
}
System.out.print("\n\nSecond Array\t: ");
for(int i=0; i<Array2.length; i++)
{
System.out.print(Array2[i]+" ");
}
System.out.print("\n\nThird Array\t: ");
for(int i=0;i<Array3.length;i++)
{
System.out.print(Array3[i]+" ");
}
}
}

Output



Video


Single Dimension Array Exercise For Students

  1. Enter elements in an array and multiply every element by 2 and then print them in reverse order.
  2. Enter elements in an array and do the sum of all elements of array.
  3. Enter elements in an array and do the sum of ODD and EVEN elements separately.
  4. Enter elements in an array and do the sum of all POSITIVE elements and Count all elements which are present on ODD positions.
  5. Initialize an array of characters and print only Vowels from them.

Matrix 
or 
Two D Array

Declaration of Double Dimension or Two D Array or Matrix

Data type  Name_of_ array[ ][ ] = new Data type [Rows][Columns];

Example:
int Matrix[][] = new int[3][3];

Initialization of Double Dimension Array

Data type  Name_of_Array [ ][ ] = {  {value1,  value2, value3},
                                                            {value4,  value5, value6},
                                                         };

Example:
int Matrix[ ][ ]={
                             {10,20,30},
                             {40,50,60}
                          }; 


Program To Initialize and Print A Matrix

class Matrix
{
public static void main(String args[])
{
//Matrix declaration and initialization
int Matrix[][]={
{10,20,30},
{40,50,60},
{70,80,90}
   };

//Printing of matrix
System.out.println("Matrix Elements Are ");
for(int i=0; i<3; i++)//Rows
{
for(int j=0; j<3; j++)//Columns
{
System.out.print(Matrix[i][j]+"  ");
}
System.out.println();
}
}
}

Output

Video



Program To Input and Print Matrix

import java.io.*;
class MatrixByUser
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

//Declaration of matrix
int Matrix[][]=new int[3][3];

//Inserting elements into matrix
System.out.println("Enter Matrix Elements ");
for(int i=0; i<3; i++)//Rows
{
for(int j=0; j<3; j++)//Columns
{
Matrix[i][j]=Integer.parseInt(br.readLine());
}
System.out.println();
}

//Printing Matrix Elements
System.out.println("Matrix Elements Are ");
for(int i=0; i<3; i++)//Rows
{
for(int j=0; j<3; j++)//Columns
{
System.out.print(Matrix[i][j]+"  ");
}
System.out.println();
}
}
}

Output

Diagonals or Alternative Elements Print of Matrix

To print perfect diagonals matrix should be a square matrix i.e. it should in the form of N*N; For example 2*2, 3*3, 4*4 etc. In rectangular matrix we can find a perfect diagonal; For example in matrix of 2*3, 4*6 a perfect diagonal can not print.






Program
class DiagonalsPrint
{
public static void main(String args[])
{
//Square Matrix Initialized
int Matrix[][]= {
{10,20,30},
{40,50,60},
{70,80,90}
};

//Getting Number of Rows and Columns
int n = Matrix.length;

System.out.println("Diagonals or Alternative Elements Are ");
for(int i=0; i<n; i++)//Rows
{
for(int j=0; j<n; j++)//Columns
{
//Printing Diagonals
if(i==j)
{
System.out.print(Matrix[i][j]);
}
//Printing Anti Diagonals
else if(i+j==n-1)
{
System.out.print(Matrix[i][j]);
}
//Printing Space Where Condition is False
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}

Output


Video

Rows Sum

Find and print the sum of each raw elements of a matrix.


class Row_Sum
{
public static void main(String args[])
{
//Square Matrix Initialized
int Matrix[][]= {
{10,20,30},
{40,50,60},
{70,80,90}
};

//Getting Number of Rows and Columns
int n = Matrix.length;
int sum;

System.out.println("Row Sum of Matrix ");
for(int i=0; i<n; i++)//Rows
{
sum=0;
for(int j=0; j<n; j++)//Columns
{
System.out.print(Matrix[i][j]+" ");
sum=sum+Matrix[i][j];
}
System.out.println("=> "+sum);
}
}
}

Output

Video

Column Sum

Find and print the sum of each column elements of a matrix.


class Column_Sum
{
public static void main(String args[])
{
//Square Matrix Initialized
int Matrix[][]= { 
{10,20,30}, 
{40,50,60}, 
{70,80,90}
};
//Getting Number of Rows and Columns
int n = Matrix.length;
int sum;

//Printing Matrix
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(Matrix[i][j]+"  ");
}
System.out.println();
}

//Printing Column Sum
for(int i=0;i<3;i++)
{
sum=0;
for(int j=0;j<3;j++)
{
sum=sum+Matrix[j][i];
}
System.out.print(sum+" ");
}
}
}

Output


Symmetric Matrix

Symmetric matrix is a square matrix which is equal to its transpose. 

In the other words, if A[][] is a square matrix with (M x M) order, then this matrix is said to be symmetric if every element at ith row and jth column is equal to element at jth row and ith column i.e A[i][j] == A[j][i].

Program
import java.util.*;
class SymmetricMatrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

//Declaration of Matrix
int Matrix[][]=new int[3][3];
int counter=0;

//Inserting Elements Into Matrix
System.out.println("Enter Matrix Elements ");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
Matrix[i][j]=sc.nextInt();
}
}

for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
//Printing Matrix
System.out.print(Matrix[i][j]+" ");

//Counting Matching Cases
if(Matrix[i][j]==Matrix[j][i])
{
counter++;
}
}
System.out.println();
}

//Printing Message
if(counter==9)
System.out.println("Matrix is Symmetric");
else
System.out.println("Matrix is Not symmetric ");
}

}

Output

Half Matrix

To print triangular half matrix it is necessary that matrix should be in square form i.e. the number of rows and column in the matrix should be equal. Dimension of matrix should be n*n.

For half matrix we check index position I and J i.e. Row and Column respectively.

Upper right triangular matrix
Upper right triangular matrix is a matrix which contain elements above principle diagonal including principle diagonal elements and rest of the elements are either 0 or blank.

To print it we check if row position is smaller than or equals to the column position (i.e. i<=j) then we will print element at that position otherwise will print 0 or blank space at rest of the position.  

Lower left triangular matrix
Lower left triangular matrix is a matrix which contain elements below principle diagonal including principle diagonal elements and rest of the elements are either 0 or blank.

To print it we check if row position is greater than or equals to the column position (i.e. i>=j) then we will print element at that position otherwise will  print 0 or blank at rest of the position.


Upper left triangular matrix
Upper left triangular matrix is a matrix which contain elements above secondary diagonal including secondary diagonal elements and rest of the elements are either 0 or blank.

To print it we check if row+column position sum is smaller than or equals to matrix size-1              i.e. (i+j) <= (n-1) then we will print element at that position otherwise will print 0 or blank space at rest of the position. 


Lower right triangular matrix
Lower right triangular matrix is a matrix which contain elements below secondary diagonal including secondary diagonal elements and rest of the elements are either 0 or blank.

To print it we check if row+column sum is greater than or equals to matrix size-1 i.e. (i+j) >= (n-1)
then we will print element at that position otherwise will  print 0 or blank at rest of the position. 


Program
class HalfMatrix
{
public static void main(String args[])
{
//Matrix declaration and initialization
int Matrix[][] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
int n =  Matrix.length;

//Half Matrix Printing

System.out.println("Upper Right Triangular Matrix ");
for(int i=0; i<3; i++)
{
for(int j=0;j<3;j++)
{
//Upper Right Side Triangular Matrix Print
if(i<=j)
System.out.print(Matrix[i][j]);
else
System.out.print(" ");
}
System.out.println();
}

System.out.println("Lower Left Triangular Matrix ");
for(int i=0; i<3; i++)
{
for(int j=0;j<3;j++)
{
//Lower Left Side Triangular Matrix Print
if(i>=j)
System.out.print(Matrix[i][j]);
else
System.out.print(" ");
}
System.out.println();
}

System.out.println("Lower Right Triangular Matrix ");
for(int i=0; i<3; i++)
{
for(int j=0;j<3;j++)
{
//Lower Right Side Triangular Matrix Print
if(i+j>=n-1)
System.out.print(Matrix[i][j]);
else
System.out.print(" ");
}
System.out.println();
}

System.out.println("Upper Left Triangular Matrix ");
for(int i=0; i<3; i++)
{
for(int j=0;j<3;j++)
{
//Upper Left Side Triangular Matrix Print
if(i+j<=n-1)
System.out.print(Matrix[i][j]);
else
System.out.print(" ");
}
System.out.println();
}
}
}

Output



Matrix Multiplication Program

Getting a single matrix by multiplying two matrices with * binary operator is called matrix multiplication. 

If two matrices x of size m*n and y of size n*p are multiplied than resultant matrix will be z of size m*p.
for example if first matrix is Matrix1 [3][2] and second matrix is Matrix2 [2][3] than third matrix will be Matrix3[3][3].
The concept is this:

First raw first element of first matrix will multiply with first column first element of second matrix and will store in sum variable after that second element of first raw of first matrix will multiply second element of first column of second matrix and result will added to sum variable along with previous value of sum. 

This process is repeated for every element of first raw of first matrix and every element of first column of second matrix.

At the end of this process sum that we will get will be first element of third matrix.

Then same process is repeated to get second element. This time first raw elements of first matrix will multiply by second column elements of second matrix and sum of these statement will generate second element of third matrix and so on.

When we switch to second raw of third matrix for this second raw's element of first matrix will multiply with second column elements of second matrix.

same process will repeated for all rows and columns of matrix.

Program
class Matrix_Multiplication
{
public static void main(String args[])
{
//Matrix declaration and initialization 
int Matrix1[][] = {
{1,2,3},
{4,5,6},
{7,8,9}
  };
  
int Matrix2[][] = {
{1,2,3},
{4,5,6},
{7,8,9}
  };
  
int Matrix3[][] = new int[3][3];
//Matrix multiplication
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
Matrix3[i][j]=0;
for(int k=0; k<3; k++)
{
Matrix3[i][j]=Matrix3[i][j]+Matrix1[i][k]*Matrix2[k][j];
}
}
}

//Printing Matrix1
System.out.println("\nFirst Matrix");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(Matrix1[i][j]+"\t");
}
System.out.println();
}
//Printing Matrix2
System.out.println("\nSecond Matrix");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(Matrix2[i][j]+"\t");
}
System.out.println();
}
//Printing Matrix3
System.out.println("\nThird Matrix3 after Multiplication \n");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(Matrix3[i][j]+"\t");
}
System.out.println();
}
}
}

Output


Transpose of Matrix

Converting rows of a matrix into columns and columns of a matrix into row is called transpose of a matrix. Transpose of a matrix MAT[][] is obtained by changing MAT[i][j] to MAT[j][i]. 

Transposition of matrix can do in two ways:

One by copy the contents of the original matrix to the new matrix such that elements in the A[j][i] position of the original matrix should be copied to the B[i][j] position of the new matrix.

Second by printing original matrix such that printing command will replace from  MAT[i][j]  to  MAT[j][i]
      
import java.util.*;
class TransposeMatrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

//Matrix declaration
int Matrix[][]= new int[3][3];

//Inserting elements into matrix
System.out.println("\nEnter Elements of Matrix ");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
Matrix[i][j]=sc.nextInt();
}
}

//Printing real Matrix
System.out.println("\nMatrix before transposition is ");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(Matrix[i][j]+"  ");
}
System.out.println();
}

//Transposing Matrix
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
if(i<j)
{
int tempVariable = Matrix[j][i];
Matrix[j][i]=Matrix[i][j];
Matrix[i][j]=tempVariable;
}
}
}

//Printing transposed Matrix
System.out.println("\nMatrix after transposition is ");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(Matrix[i][j]+"  ");
}
System.out.println();
}
}
}

Output


Swapping between First and Last Column of Matrix

Swapping of first and last column of matrix can be achieved by exchange the outermost columns of matrix such that the first column becomes the last and the last column becomes the first. All other columns should be left unchanged.

Program
class First_Last_Column_Swap
{
public static void main(String args[])
{
//Matrix declaration int initialization
int Matrix[][]= { {10,20,30}, {40,50,60}, {70,80,90}};

//Printing real Matrix
System.out.println("\nMatrix before column swapping ");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(Matrix[i][j]+"  ");
}
System.out.println();
}

//getting number of rows of matrix
int n=Matrix.length;

//swapping column of matrix
for(int i=0; i<n; i++)
{
int tempVariable = Matrix[i][0];
Matrix[i][0]=Matrix[i][n-1];
Matrix[i][n-1]=tempVariable;
}

//Printing matrix after swapping of column
System.out.println("\nMatrix after column swapping\n");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(Matrix[i][j]+"  ");
}
System.out.println();
}
}
}

Output



Swapping between First and Last Rows of Matrix

Swapping of first and last raw of matrix can be achieved by exchange the outermost rows of matrix such that the first raw becomes the last and the last raw becomes the first. All other rows should be left unchanged.

Program
class First_Last_Rows_Swap
{
public static void main(String args[])
{
//Matrix declaration int initialization
int Matrix[][]= { {10,20,30}, {40,50,60}, {70,80,90}};

//Printing real Matrix
System.out.println("\nMatrix before rows swapping ");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(Matrix[i][j]+"  ");
}
System.out.println();
}

//getting matrix number of rows
int n=Matrix.length;

//swapping rows of matrix
for(int i=0; i<3; i++)
{
int tempVariable = Matrix[0][i];
Matrix[0][i]=Matrix[n-1][i];
Matrix[n-1][i]=tempVariable;
}

//Printing matrix after swapping of rows
System.out.println("\nMatrix after rows swapping\n");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(Matrix[i][j]+"  ");
}
System.out.println();
}
}
}

Output


Replacement of each column with its next column in 3*3 Matrix

class Replacement_Columns
{
public static void main(String args[])
{
//Matrix declaration int initialization
int Matrix[][]= { {10,20,30}, {40,50,60}, {70,80,90}};

//Printing real Matrix
System.out.println("\nMatrix before column replacement ");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(Matrix[i][j]+"  ");
}
System.out.println();
}

//getting total number of matrix column
int column=3;

//column will decrease by 1 because of index which start from 0
column=column-1;

//replacement columns in 3*3 matrix
for(int i=0; i<3; i++)//Loop for rows
{
int tempVariable = Matrix[i][column];
Matrix[i][column]=Matrix[i][column-1];
Matrix[i][column-1]=Matrix[i][column-2];
Matrix[i][column-2]=tempVariable;
}

//Printing matrix after swapping of column
System.out.println("\nMatrix after column swapping\n");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(Matrix[i][j]+"  ");
}
System.out.println();
}
}
}

Output


Find Saddle Point of a matrix

Saddle Point is an element of matrix which is minimum element of its raw but is maximum element of its column.
For Example:

In above matrix 40 is the Saddle Point because it is minimum element of its row and is maximum element of its column.

Program
import java.io.*;
class Saddle_Point
{
public static void main(String args[]) throws IOException
{
DataInputStream in = new DataInputStream(System.in);

//Matrix declaration
int Matrix[][]=new int[3][3];
int row[]=new int[3];
int col[]=new int[3];

//Inserting element into matrix
System.out.println("Enter Matrix Elements ");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
Matrix[i][j]=Integer.parseInt(in.readLine());
}
}

//Printing element into matrix
System.out.println("\nMatrix Elements ");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(Matrix[i][j]+" ");
}
System.out.println();
}


int minrow=0,maxcol=0;
int k=0,l=0;
int i,j;

//Getting saddle point of matrix
for(i=0; i<3; i++)
{
minrow=Matrix[i][0];
maxcol=Matrix[0][i];

for(j=0; j<3; j++)
{
if(minrow>Matrix[i][j])
{
minrow=Matrix[i][j];
}
}

for(j=0; j<3; j++)
{
if(maxcol<Matrix[j][i])
{
maxcol=Matrix[j][i];
}
}
row[k++]=minrow;
col[l++]=maxcol;
}

boolean found=false;

outerLoop:
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
if(row[i]==col[j])
{
found=true;
break outerLoop;
}
}
}

//Printing result message
if(found==true)
{
System.out.println("\nSaddle Point of Matrix is "+row[i]);
}
else
{
System.out.println("\nNo Saddle Point Exists in the Matrix");
}
}
}

Output

Spiral Printing of Matrix

class Spiral_Print
{
public static void main(String args[])
{
//Matrix Initialization
int arr[][]={
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
};
//Spiral Printing of Matrix
for(int i=0; i<4; i++)
{
if(i==0)
{
for(int j=0; j<4; j++)
{
System.out.print(arr[i][j]+" ");
}
}
if(i==1 || i==2)
{
System.out.print(arr[i][3]+" ");
}
if(i==3)
{
for(int j=3; j>=0; j--)
{
System.out.print(arr[i][j]+" ");
}
}
}
System.out.print(arr[2][0]+"  ");
for(int i=0; i<4; i++)
{
if(i==1)
{
for(int j=0; j<3; j++)
{
System.out.print(arr[i][j]+"  ");
}
}
}
System.out.print(arr[2][2]+"   ");
System.out.print(arr[2][1]+"   ");
}
}

Output

Transpose of matrix at 90 degree

class MatrixTransposeAt90
{
public static void main(String args[])
{
//Matrix Initialization
int Matrix[][]={
{1,2,3},
{4,5,6},
{7,8,9}
   };

//Printing of Matrix Elements
System.out.println("Real Matrix ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(Matrix[i][j]+"  ");
}
System.out.println();
}
//Transpose of matrix at 92 degree
System.out.println("\n\nTranspose Matrix ");
int c=0;
for(int i=0;i<3;i++)
{
int r=2;
for(int j=0; j<3;j++)
{
System.out.print(Matrix[r--][c]+"  ");
}
System.out.println();
c++;
}
}
}

Output

Exercise 

  1. Enter a matrix of 3*3 order and print it in reverse order.
  2. Enter a matrix of 3*4 order and do the sum of all even and odd elements separately.
  3. Initialize a 3*3 order matrix and print center element.
  4. Print the following shape of matrix from an array.
          100000
          120000
          123000
          123400
          123450
          123456
      5. If an array is 1,2,3 than resultant Two D Array should be?
          1 2 3
          1 2 0
          1 0 0

Raw Major Formula

Add [I, J] = BA+W [ C (I-Lr) + (J-Lc) ]

OR

LOCATION A[i,j] = BA + W[C(I-1)+(J-1)] where C are columns of base array

OR

Add [I, J] = BA+W [ C (I) + (J) ]

OR

Location=Base Address + Byte ((base array column*searched array row) + searched array column)

BA   = Base Address
W    =  Bytes required 
C     =  Column of Base Array (Uc-Lc)+1
I      =  Raw of Search Array
Lr    =  Lower Limit of raw of Base Array
J      =  Column of search array
Lc   =  Lower limit of column of Base Array

Example
An array T[90][100] is stored in memory with each element requiring 4 bytes of storage. If the first element ARR[0][0] is stored at the location 7200, calculate the location of ARR[10][40] when the array is stored row major wise.

Base Array is T [90] [100]
Base Address is =7200
Byte of each elements is =4
Location is to find of T [10] [40]

Solution
Location=7200+4(100*10 + 40)
              =7200+4(1000+40)
              =7200+4(1040)
              =7200+4160
              =11360

Exercise

Question 1: An array ARR[10][5] is stored in memory with each element requiring 2 bytes of storage. If the first element ARR[0][0] is stored at the location 1250, calculate the location of ARR[5][4] when the array is stored row major wise.

Question 3: A square matrix M[][] of size 10 is stored in the memory with each element requiring 4 bytes of storage. If the base address at M[0][0] is 1840, determine the address at M[4][8] when the matrix is stored in row major wise.

Question 2: In an array of real numbers ARR[25][25], ARR[1][1] is stored in location 1000. Find the address of ARR[12][12] when the array is stored row major wise. Assume each real number requires 4 bytes.

Question 4: X is 2-D array [10*5] each element of the array is stored in 2 Memory locations. If x[1,1] begins at address 150, find the location of X[3,4].

Question 5: A two dimensional array defined as x[3…6, -2…2] requires 2 bytes of storage space for each element. If the array is stored in row major order, determine the address of X[5,1], given the base address as 1200.

Question 6: A 2-D array defined as A[4..7, -1..3] requires 2 words of storage space for each element. If the array is stored in Row-Major form, calculate the address of A[6,2] given the base address as 100

Question 7: A two D array defined as X[3..6, -2…2] requires 2 bytes of storage space for each element. If the array is stored in Row Major order, determine the address of X[5,1] given the base address as 1200

Question 8: each element of an array data[1…10][1…10] requires 8 bytes of storage. If base address of array Data is 2000, determine the location of data[4][5]  (ans: 2272)

Question 9: an array val[1..15][1..10] is stored in the memory with each element requiring 4 bytes of storage. If the base address of array Val is 1500, determine the location of val[12][9] (ans: 1972)

Question 10: calculate the address of X[4,3] in a two dimensional array x[1..5, 1..4] stored in row major order in the main memory. Assume the base address to be 1000 and the each element require 4 bytes. (ans : 1056)

Base Address Calculation in Raw Major Formula

Location=Base Address + Byte ((base array column*searched array row) + searched array column)

Example:
An array AR[-4...6,-2....12], stores elements in Row Major wise, with address AR[2][3] AS 4142. If each element requires 2 bytes of storage, find the Base address.

Base Address = ?
Base Array is AR [-4…6, -2…12]
Array element AR [2,3] address is 4142
Byte of each element (w) = 2
Base Array Column= Uc-Lc+1
                                     (Upper Column-Lower Column+1)
                                  = 12-(-2)+1=15
Solution
4142    = BA + 2 [(12-(-2)+1)*2+3]
4142    = BA + 2 [15*2+3]
4142    = BA + 2 [33]
4142    = BA + 2*33
4142    = BA + 66
BA      = 4142 – 66 = 4076

Question 1: Each element of an array A[20][10] requires 2 bytes of storage. If the address of A[6][8] is 4000, find the base address at A[0][0] when the array is stored row major wise.

Question 2: The array A[20] [10] is stored in the memory with each element requiring one byte of storage if the base address of A is C0. Determine C0 when the location of A[10] [5] is 2000.

Question 3: An array MAT [20] [10] is stored in the memory along the row with each element occupying 4 bytes of memory. Find out the base address and the address of element MATE[10][5] if the location of MAT [3][7] is stored at the address 1000.

Question 4: An array arr[3][15] is stored in the memory along the row with each element occupying 4 bytes. Find out the base address and the address of an element arr[20][5], if the location of arr[2][2] is stored at the address 3000

Question 5: An array [15][20] is stored in the memory along with raw with each element occupying 4 bytes. Find out the base address and address of the element arr[3][2], if the element arr[5][2] is stored at the address 15000.

Column Major Formula

Add [I, J] = BA+W [ R (J-Lc) + (I-Lr) ]

OR

LOCATION A[i,j] = BA + W[R(J-1)+(I-1)] where R are rows of base array

OR

Add [I, J] = BA+W [ R (J) + (I) ]

OR

Location=Base Address + Byte ((base array raw*searched array column) + searched array raw)

BA   = Base Address
W    =  Bytes required 
R    =   Raw of Base Array (Ur-Lr)+1
I      =  Raw of Search Array
Lr    =  Lower Limit of raw of Base Array
J      =  Column of search array
Lc   =  Lower limit of column of Base Array

Example
Each element of an array T[90][100] requires 4 bytes of storage. If the base address of T[0][0] is 7200, determine the location of T[10][40] when the array is stored as column major wise.

Base Array is T [90] [100]
Base Address is =7200
Byte of each elements is =4
Location is to find of T [10] [40]

Solution
Location=7200+4[90(40-0)+(10-0)]
              =7200+4[90*40+10]
              =7200+4[3610]
              =7200+14440

              =21640

Question 1: Each element of an array Data[30][40] requires 4 bytes of storage. If the base address of Data[0][0] is 3000, determine the location of Data[10][10] when the array is stored as column major wise.

Question 2: A character array B[7][6] has a base address 1046 at 0,0. Calculate the address at B[2][3] if the array is stored Column Major wise. Each character requires 2 bytes of storage.

Question 3: Each element of an array X[-15…10,15…40] requires one byte of storage. If the array is stored in column major order with the beginning location 1500, determine the location of X[5,20].

Question 4: For an array of real numbers x [− 6… 8, -12… 20], find the address of x [5] [4] if x [1] [1] is stored in location 100 in the column major order. Assume that each element requires 4 bytes.

Question 5: For an array of real numbers x [− 6… 8, -12… 20], find the address of x [5] [4 ], if x [1] [1] is stored in location 100 in the column major order. Assume that each element requires 4 bytes.

Base Address Calculation in Column Major Formula

Location=Base Address + Byte ((base array raw*searched array column) + searched array raw)

Example:
An array AR[-4...6,-2....12], stores elements in Row Major wise, with address AR[2][3] AS 4142. If each element requires 2 bytes of storage, find the Base address.

Base Address = ?
Base Array is AR [-4…6, -2…12]
Array element AR [2,3] address is 4142
Byte of each element (w) = 2
Base Array Column= Ur-Lr+1
                                     (Upper Raw-Lower Raw+1)
                                  = 6-(-4)+1 = 11
Solution
4142    = BA + 2 [(6-(-4)+1)*3+2]
4142    = BA + 2 [11*3+2]
4142    = BA + 2 [33+2]
4142    = BA + 2*35
4142    = BA + 70
BA      = 4142 – 70 = 4072

Question 1: Each element of an array A[20][10] requires 2 bytes of storage. If the address of A[6][8] is 4000, find the base address at A[0][0] when the array is stored column major wise.

Question 2: The array A[20] [10] is stored in the memory along with column with each element requiring one byte of storage if the base address of A is C0. Determine C0 when the location of A[10] [5] is 2000.

Question 3: An array MAT [20] [10] is stored in the memory along the column with each element occupying 4 bytes of memory. Find out the base address and the address of element MATE[10][5] if the location of MAT [3][7] is stored at the address 1000.

Question 4: An array arr[3][15] is stored in the memory along the column with each element occupying 4 bytes. Find out the base address and the address of an element arr[20][5], if the location of arr[2][2] is stored at the address 3000

Question 5: An array [15][20] is stored in the memory along with column with each element occupying 4 bytes. Find out the base address and address of the element arr[3][2], if the element arr[5][2] is stored at the address 15000.

Byte Calculation

Question: Each element of an array arr[15][20] requires ‘W’ bytes of storage. If the address of   arr[6][8] is 4440 and the Base Address of arr[1][1] is 4000, find the width ‘W’ of each cell in the array arr[][] when the array is stored as COLUMN Major wise.

Add [I, J] = BA+W [ R (J-Lc) + (I-Lr) ]

Solution
4440           = 4000 +W [15(8-1)+(6-1)]
4440           = 4000 + W[15*7+5]
4400           = 4000 + W*110
4440-4000  = 110W
440             = 110W
W               = 440/110
W               = 4

Raw Calculation

Question: A matrix B[10][7] is stored in the memory with each element requiring 2 bytes of storage. If the Base address at B[x][1] is 1012 and the address B[7][3] is 1060, determine the value x where the matrix is stored in column major wise.

Add [I, J] = BA+W [ R (J-Lc) + (I-Lr) ]

Solution
1060 = 1012 + 2[10(3-1)+(7-x)]
1060 = 1012 +2[10*2 +(7-x)]
1060 = 1012+2[20+(7-x)]
1060 = 1012+40+14-2x
1060 = 1052+14-2x
1060 = 1066-2x
2x     = 1066-1060
2x     = 6
x       = 6/2
x       = 3

Column Calculation

Question: A matrix B[10][7] is stored in the memory with each element requiring 2 bytes of storage. If the Base address at B[1][y] is 1012 and the address B[7][3] is 1060, determine the value y where the matrix is stored in column major wise.

Add [I, J] = BA+W [ R (J-Lc) + (I-Lr) ]

Solution
1060 = 1012 +2[10(3-y)+(7-1)]
1060 = 1012 +2[10*(3-y) +(6)]
1060 = 1012+2[(30-10y)+6]
1060 = 1012+60+20y-12
1060 = 1012+72-20y
1060 = 1084-20y
20y   = 1084-1060
20y   = 24
y       = 24/20
y       = 1
         
Question: A square matrix A[m x m] is stored in the memory with each element requiring 2 bytes of storage. If the base address A[1][1] is 1098 and the address at A[4][5] is 1144, determine the order of the matrix A[mxm] when the matrix is stored column major wise.   

Comments

Popular posts from this blog

Structure In C++ Programming

Definition Structure is collection of heterogeneous (different) data types. It is declared by struct keyword. All members of structure by default are public and can be access outside structure by reference variable (structure variable).   Structure elements stored at contiguous memory location. Structure is similar to class except it does not have access specifier (Public, Private, Protected). For example if we want to collect information of a student (Name, Roll Number, Class etc) than we can group these different type of information into a single unit by structure. Syntax struct <structure name> {     element 1;     element 2;     ------------     ------------ }; Declaration of Structure Structure is declared by struct keyword followed by an identifier i.e. the structure name.for example: struct Student Structure body starts with an opening curly braces and ends with closing curly bra...

Control Structure in Java Programming

Control Structure Control structure or control statements are the statements which control the flow of program execution. Control Structures are just a way to specify flow of control in programs. Any algorithm or program can be more clear and understood if they use self-contained modules called as logic or control structures. It basically analyzes and chooses in which direction a program flows based on certain parameters or conditions. These are of four types. 1. Conditional or Decision making statement. Example : if, if-else, nested if-else 2. Selection statement and branching. Example : else if ladder, switch, nested switch 3. Iteration or looping. Example for, while, do-while, for each loop 4. Jump statement. Example: break, continue Decision Making and Branching Decision Making or Conditional Statements In programming we face some situations where we want certain block of code to be executed when some condition is fulfilled. Decision maki...