Blog

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>
<html>
<head>
	<title>Matrix using Single Loop</title>
	<meta charset="UTF-8">
</head>
<body style="text-align: center;margin-top: 15%">
	<h2>Rows</h2>
	<input type="text" name="rows" id="row">
	<h2>Columns</h2>
	<input type="text" name="columns" id="column">
	<br>
	<br>
	<input onclick="createMatrix()" type="submit" value= "Generate">
	<br>
	<br>
	<div id="output">
	</div>

	<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+' ';

				}else{

					output.innerHTML += i+' ';

				}
				
			}
		}
	</script>
</body>
</html>

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