Home  /  Coding   /  How To Create Matrix Using Single Loop | AV Coding

How To Create Matrix Using Single Loop | AV Coding

Hey guys welcome to AV Coding

Today we will learn how to create 2d matrix of m x n using just a single loop logic in any language.

The complete code is explained in the video below and the source code is provided below.

<!DOCTYPE html>
	<title>Matrix using Single Loop</title>
	<meta charset="UTF-8">
<body style="text-align: center;margin-top: 15%">
	<input type="text" name="rows" id="row">
	<input type="text" name="columns" id="column">
	<input onclick="createMatrix()" type="submit" value= "Generate">
	<div id="output">

	<script type="text/javascript">
		function createMatrix(){
			var row = document.getElementById("row").value;

			var column = document.getElementById("column").value;

			var output = document.getElementById('output');

			output.innerHTML = '';

			for(var i = 1; i<=(row*column); i++){

				if((i-1)%column == 0){

					output.innerHTML += '<br>';
					output.innerHTML += i+' ';


					output.innerHTML += i+' ';


Hope You Liked This Blog. Share, Comment, Subscribe And Press The Bell Icon In The Bottom Right Side For More Code Feeds.