Square of diagonal elements in matrix

Here given code implementation process.

/*
  C Program 
+ Square of diagonal elements in matrix
*/
#include<stdio.h>
#define ROW 3
#define COL 3

void diagonal_squares(int data[ROW][COL])
{
  printf("\n First :\n");
  for (int i = 0; i < ROW; ++i)
  {
    printf("%4d",data[i][i]*data[i][i]);
  }
 
  printf("\n Second :\n");
  for (int i = 0; i < ROW; ++i)
  {
    printf("%4d",data[(ROW-1)-i][i]*data[(ROW-1)-i][i]);
  }

}
int main(){

  int arr[ROW][COL]= { 
    { 1,2,3}, 
    { 4,5,6}, 
    { 9,8,7}, 
  }; 


  diagonal_squares(arr);

  return 0;
}

Output

 First :
   1  25  49
 Second :
  81  25   9
/*
  C++ Program
  Square of diagonal elements in matrix
*/
#include<iostream>
#define SIZE 3
using namespace std;

class MyMatrix {
	public:
		int size;
	MyMatrix() {
		//Assume that given matrix size is NXN 
		this->size = SIZE;
	}
	void diagonal_squares(int matrix[][SIZE]) {
		cout << "\n First :\n";
		for (int i = 0; i < this->size; ++i) {
			cout << " " << matrix[i][i] *matrix[i][i];
		}
		cout << "\n Second :\n";
		for (int i = 0; i < this->size; ++i) {
			cout << " " << matrix[(this->size - 1) - i][i] *matrix[(this->size - 1) - i][i];
		}
	}
};
int main() {
	int matrix[][SIZE] = {
		{
			1,
			2,
			3
		},
		{
			4,
			5,
			6
		},
		{
			9,
			8,
			7
		}
	};
	MyMatrix obj ;
	obj.diagonal_squares(matrix);
	return 0;
}

Output

 First :
 1 25 49
 Second :
 81 25 9
/*
  Java Program
  Square of diagonal elements in matrix
*/
public class MyMatrix {

  public int size;


  public MyMatrix(int [][]matrix)
  {
    //Assume that given matrix size is NXN 
    this.size = matrix.length;

  }

  public void diagonal_squares(int [][]matrix)
  {
    System.out.print("\n First :\n");
    for (int i = 0; i < this.size; ++i)
    {
      System.out.print("  "+matrix[i][i]*matrix[i][i]);
    }
   
    System.out.print("\n Second :\n");
    for (int i = 0; i < this.size; ++i)
    {
      System.out.print("  "+matrix[(this.size-1)-i][i]*matrix[(this.size-1)-i][i]);
    }

  }
  public static void main(String[] args) 
  {
    //Define matrix element
    int [][]matrix =
    { 
      { 1,2,3}, 
      { 4,5,6}, 
      { 9,8,7}
    }; 
    MyMatrix obj = new MyMatrix(matrix);

    obj.diagonal_squares(matrix);
  }
}

Output

 First :
 1 25 49
 Second :
 81 25 9
/*
  C# Program
  Square of diagonal elements in matrix
*/
using System;

public class MyMatrix {
	int size;
	MyMatrix(int[,] matrix) {
		//Assume that given matrix size is NXN 
		this.size = matrix.GetLength(0);
	}
	public void diagonal_squares(int[,] matrix) {
		Console.Write("\n First :\n");
		for (int i = 0; i < this.size; ++i) {
			Console.Write(" " + matrix[i,i] * matrix[i,i]);
		}
		Console.Write("\n Second :\n");
		for (int i = 0; i < this.size; ++i) {
			Console.Write(" " + matrix[(this.size - 1) - i,i] * matrix[(this.size - 1) - i,i]);
		}
	}
	public static void Main(String[] args) {
		int[,]
		//Define matrix element
		matrix = {
			{
				1,
				2,
				3
			},
			{
				4,
				5,
				6
			},
			{
				9,
				8,
				7
			}
		};
		MyMatrix obj = new MyMatrix(matrix);
		obj.diagonal_squares(matrix);
	}
}

Output

 First :
 1 25 49
 Second :
 81 25 9
<?php
/*
  Php Program
  Square of diagonal elements in matrix
*/
class MyMatrix {
	public $size;

	function __construct($matrix) {
		//Assume that given matrix size is NXN 
		$this->size = count($matrix);
	}
	public 	function diagonal_squares($matrix) {
		echo("\n First :\n");
		for ($i = 0; $i < $this->size; ++$i) {
			echo(" ". $matrix[$i][$i] *$matrix[$i][$i]);
		}
		echo("\n Second :\n");
		for ($i = 0; $i < $this->size; ++$i) {
			echo(" ". $matrix[($this->size - 1) - $i][$i] *$matrix[($this->size - 1) - $i][$i]);
		}
	}
}

function main() {
	//Define matrix element
	$matrix = array(
      array(1, 2, 3), 
      array(4, 5, 6), 
      array(9, 8, 7));
	$obj = new MyMatrix($matrix);
	$obj->diagonal_squares($matrix);

}
main();

Output

 First :
 1 25 49
 Second :
 81 25 9
/*
  Node Js Program
  Square of diagonal elements in matrix
*/
class MyMatrix {
	constructor(matrix) {
		//Assume that given matrix size is NXN 
		this.size = matrix.length;
	}
	diagonal_squares(matrix) {
		process.stdout.write("\n First :\n");
		for (var i = 0; i < this.size; ++i) {
			process.stdout.write(" " + matrix[i][i] *matrix[i][i]);
		}

		process.stdout.write("\n Second :\n");
		for (var i = 0; i < this.size; ++i) {
			process.stdout.write(" " + matrix[(this.size - 1) - i][i] *matrix[(this.size - 1) - i][i]);
		}
	}
}

function main(args) {
	//Define matrix element
	var matrix = [
		[1, 2, 3],
		[4, 5, 6],
		[9, 8, 7]
	];
	var obj = new MyMatrix(matrix);
	obj.diagonal_squares(matrix);
}

main();

Output

 First :
 1 25 49
 Second :
 81 25 9
# Python 3 Program
# Square of diagonal elements in matrix
class MyMatrix :
	
	def __init__(self, matrix) :
		# Assume that given matrix size is NXN 
		self.size = len(matrix)
	
	def diagonal_squares(self, matrix) :
		print("\n First :\n", end = "")
		i = 0
		while (i < self.size) :
			print(" ", matrix[i][i] * matrix[i][i], end = "")
			i += 1
		
		print("\n Second :\n", end = "")
		i = 0
		while (i < self.size) :
			print(" ", matrix[(self.size - 1) - i][i] * matrix[(self.size - 1) - i][i], end = "")
			i += 1
		
	

def main() :
	matrix = [
		[1, 2, 3],
		[4, 5, 6],
		[9, 8, 7]
	]
	obj = MyMatrix(matrix)
	obj.diagonal_squares(matrix)


if __name__ == "__main__":
	main()

Output

 First :
  1  25  49
 Second :
  81  25  9
# Ruby Program
# Square of diagonal elements in matrix
class MyMatrix 
	# Define the accessor and reader of class MyMatrix
    attr_reader :size
    attr_accessor :size
	def initialize(matrix) 
		# Assume that given matrix size is NXN 
		self.size = matrix.length
	end
	def diagonal_squares(matrix) 
		print("\n First  :\n")
		i = 0
		while (i < self.size) 
			print(" ", matrix[i][i] * matrix[i][i])
			i += 1
		end
		print("\n Second  :\n")
		i = 0
		while (i < self.size) 
			print(" ", matrix[(self.size - 1) - i][i] * matrix[(self.size - 1) - i][i])
			i += 1
		end
	end
end
def main() 
	matrix = [
		[1, 2, 3],
		[4, 5, 6],
		[9, 8, 7]
	]
	obj = MyMatrix.new(matrix)
	obj.diagonal_squares(matrix)
end


main()

Output

 First  :
 1 25 49
 Second  :
 81 25 9
/*
  Scala Program
  Square of diagonal elements in matrix
*/
class MyMatrix(var size: Int) {

	def this(matrix: Array[Array[Int]]) {
		//Assume that given matrix size is NXN 
		this(matrix.length);
	}
	def diagonal_squares(matrix: Array[Array[Int]]): Unit = {
		print("\n First :\n");
		var i: Int = 0;
		while (i < this.size) {
			print(" " + matrix(i)(i) * matrix(i)(i));
			i += 1;
		}
		print("\n Second :\n");
		i = 0;
		while (i < this.size) {
			print(" " + matrix((this.size - 1) - i)(i) * matrix((this.size - 1) - i)(i));
			i += 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val matrix: Array[Array[Int]] = Array(
			Array(1, 2, 3),
			Array(4, 5, 6),
			Array(9, 8, 7));
		val obj: MyMatrix = new MyMatrix(matrix);
		obj.diagonal_squares(matrix);
	}
}

Output

 First :
 1 25 49
 Second :
 81 25 9
/*
  Swift Program
  Square of diagonal elements in matrix
*/
class MyMatrix {
	var size: Int;
	init(_ matrix: [[Int]]) {
		//Assume that given matrix size is NXN 
		self.size = matrix.count;
	}
	func diagonal_squares(_ matrix: [[Int]]) {
		print("\n First :\n", terminator: "");
		var i: Int = 0;
		while (i < self.size) {
			print(" ", matrix[i][i] * matrix[i][i], terminator: "");
			i += 1;
		}
		print("\n Second :\n", terminator: "");
		i = 0;
		while (i < self.size) {
			print(" ", matrix[(self.size - 1) - i][i] * matrix[(self.size - 1) - i][i], terminator: "");
			i += 1;
		}
	}
}
func main() {
	let matrix: [
		[Int]
	] = [
		[1, 2, 3],
		[4, 5, 6],
		[9, 8, 7]
	];
	let obj: MyMatrix = MyMatrix(matrix);
	obj.diagonal_squares(matrix);
}
main();

Output

 First :
  1  25  49
 Second :
  81  25  9


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved