- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Print an Identity Matrix
The steps to print an identity matrix using Golang is as follows :
- Take a value from the user and store it in a variable, n.
- Use two for loops where the value of j ranges between the values of 0 and n-1 and the value of i also ranges between 0 and n-1.
- Print the value of 1 when i is equal to j, and 0 otherwise.
Case 1:
Enter a number: 4 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Case 2:
Enter a number: 5 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
Example
package main import "fmt" func main(){ var n int fmt.Print("Enter a number: ") fmt.Scanf("%d", &n) for i:=0; i<n; i++{ for j:=0; j<n; j++{ if i == j{ fmt.Print("1 ") } else { fmt.Printf("0 ") } } fmt.Println() } }
Output
Enter a number: 5 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
- Related Articles
- Python Program to Print an Identity Matrix
- Swift program to Print an Identity Matrix
- Golang program to print right diagonal matrix
- Golang program to print the left diagonal matrix
- Golang Program to Print Boundary Elements of a Matrix
- Golang program to print an array?
- Swift program to check if a given square matrix is an Identity Matrix
- Golang Program to Print an Inverted Star Pattern
- Program for Identity Matrix in C
- How to create an identity matrix using Numpy?
- Python PyTorch – How to create an Identity Matrix?
- How to Display an Identity Matrix in Java?
- Golang Program To Rotate Matrix Elements
- Golang program to print a slice
- Golang program to print struct variables

Advertisements