

{"id":116563,"date":"2023-08-16T19:11:43","date_gmt":"2023-08-16T13:41:43","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=116563"},"modified":"2023-08-16T19:11:30","modified_gmt":"2023-08-16T13:41:30","slug":"c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/","title":{"rendered":"C Program to Find Diagonal Upper and Lower Triangle of Matrix"},"content":{"rendered":"<p>Matrices are fundamental mathematical structures used in various fields, including computer science and data analysis. They offer a convenient way to represent and manipulate data in a tabular format. One common task involving matrices is finding the diagonal upper and diagonal lower triangles. In this article, we will discuss a C Program to Find Diagonal Upper and Lower Triangle of Matrix. Let&#8217;s start!!!<\/p>\n<h3>What is Diagonal Upper and Lower Triangle of Matrix<\/h3>\n<p>To begin, let&#8217;s understand what the diagonal upper and diagonal lower triangles of a matrix are. Consider a square matrix, where the number of rows is equal to the number of columns. The diagonal elements of a matrix are those elements that lie on the principal diagonal, which runs from the top left to the bottom right of the matrix. The diagonal upper triangle consists of all the elements above the principal diagonal, whereas the diagonal lower triangle contains all the elements below the principal diagonal.<\/p>\n<h3>Implementation of C Program to find Diagonal Upper and Lower Triangle of Matrix<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\n#define SIZE 100\r\nvoid findDiagonalTriangles (int matrix[][SIZE], int n) {\r\nint i, j;\r\n\/\/ Diagonal Upper Triangle\r\nprintf(\"Diagonal Upper Triangle:\\n\");\r\nfor (i = 0; i &lt; n; i++) {\r\nfor (j = 0; j &lt; n; j++) {\r\nif (j &gt; i) {\r\n}}}\r\nprintf(\"%d\", matrix[i][j]);\r\nprintf(\"\\n\");\r\n\/\/ Diagonal Lower Triangle\r\nprintf(\"Diagonal Lower Triangle:\\n\");\r\nfor (i = 0; i &lt; n; i++) {\r\nfor (j = 0; j &lt; n; j++) { if (i &gt; j) {\r\nprintf(\"%d\", matrix[i][j]);\r\n}}\r\nprintf(\"\\n\");\r\n}\r\nint main() {\r\nint matrix[SIZE][SIZE];\r\nint n, i, j;\r\nprintf(\"Enter the size of the matrix: \");\r\nscanf(\"%d\", &amp;n);\r\nprintf(\"Enter the elements of the matrix:\\n\"); for (i = 0; i &lt; n; i++) {\r\nfor (j = 0; j &lt; n; j++) {\r\nscanf(\"%d\", &amp;matrix[i][j]);\r\n}\r\nfindDiagonal Triangles (matrix, n);\r\nreturn 0;\r\n<\/pre>\n<p>Let&#8217;s go through the program step by step. We start by including the necessary header file, `stdio.h`, which provides input\/output functionality. We also define a constant `SIZE` to represent the maximum size of the matrix. Feel free to modify this value as per your requirements.<\/p>\n<p>Next, we define the `findDiagonalTriangles` function. This function takes two parameters: the `matrix` itself, and `n`, which represents the size of the matrix. Inside the function, we iterate over the matrix using two nested loops to access each element. For the diagonal upper triangle, we check if the column index is greater than the row index, and if so, we print the corresponding element. Similarly, for the diagonal lower triangle, we check if the row index is greater than the column index, and print the element accordingly.<\/p>\n<p>In the `main` function, we first declare the `matrix` array and variables `n`, `i`, and `j` for matrix size and loop counters, respectively. We prompt the user to enter the size of the matrix and read the value using `scanf`. Then, we ask the user to enter the elements of the matrix, reading them into the `matrix` array.<\/p>\n<p>Finally, we call the `findDiagonalTriangles` function, passing the `matrix` and `n` as arguments. The function prints the diagonal upper and diagonal lower triangles of the matrix.<\/p>\n<p>To run this program, compile it using a C compiler, and execute the resulting binary. Enter the size of the matrix and provide the matrix elements as input. The program will display the diagonal upper and diagonal lower triangles accordingly.<\/p>\n<p><b>Output<\/b><\/p>\n<div class=\"code-output\">Enter the size of the matrix: 3<br \/>\nEnter the elements of the matrix:<br \/>\n2 3 4 5 6 7 8 9 2<br \/>\nDiagonal Upper Triangle:<br \/>\n3 4<br \/>\n7Diagonal Lower Triangle:<br \/>\n5<br \/>\n8 9<\/div>\n<p><strong>Method 2:<\/strong> <strong>Using Pointers to Find Diagonal Upper and Diagonal Lower Triangle of Matrix<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\n#define SIZE 100\r\nvoid findDiagonalTriangles(int *matrix, int n) {\r\nint i, j;\r\n\/\/ Diagonal Upper Triangle\r\nprintf(\"Diagonal Upper Triangle:\\n\");\r\nfor (i = 0; i &lt; n; i++) {\r\nfor (j = 0; j &lt; n; j++) {\r\nif (j &gt; i) {\r\n}\r\nprintf(\"%d \", *((matrix + i * SIZE) + j));\r\nprintf(\"\\n\");\r\n}\r\n}\r\n\/\/ Diagonal Lower Triangle\r\nprintf(\"Diagonal Lower Triangle:\\n\");\r\nfor (i = 0; i &lt; n; i++) {\r\nfor (j = 0; j &lt; n; j++) {\r\n}\r\nif (i &gt; j) {\r\n}\r\nprintf(\"%d\", *((matrix + i * SIZE) + j));\r\nprintf(\"\\n\");\r\n}\r\n}\r\nint main() {\r\nint\r\nmatrix[SIZE][SIZE];\r\nint n, i, j;\r\nprintf(\"Enter the size of the matrix: \");\r\nscanf(\"%d\", &amp;n);\r\nprintf(\"Enter the elements of the matrix:\\n\");\r\nfor (i = 0; i &lt; n; i++) {\r\nfor (j = 0; j &lt; n; j++) {\r\nscanf(\"%d\", &amp;matrix[i][j]);\r\n}\r\n}\r\n}\r\nfindDiagonalTriangles(&amp;matrix[0][0], n);\r\nreturn 0;\r\n<\/pre>\n<p>In this alternative implementation, we use pointers to access the elements of the matrix instead of directly indexing them. We pass a pointer to the first element of the matrix (`&amp;matrix[0][0]`) to the `findDiagonalTriangles` function. Within the function, we use pointer arithmetic to access the matrix elements. The rest of the code remains the same.<\/p>\n<p><strong>Method 3:<\/strong> <strong>Using a One-Dimensional Array<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\n#define SIZE 100\r\nvoid findDiagonalTriangles(int matrix[], int n) {\r\nint i, j;\r\n\/\/ Diagonal Upper Triangle\r\nprintf(\"Diagonal Upper Triangle:\\n\");\r\nfor (i = 0; i &lt; n; i++) {\r\nfor (j = 0; j &lt; n; j++) {\r\nif (j &gt; i) {\r\nprintf(\"%d\", matrix[i * SIZE + j]);\r\n}\r\n}\r\nprintf(\"\\n\");\r\n}\r\n\/\/ Diagonal Lower Triangle\r\nprintf(\"Diagonal Lower Triangle:\\n\"); for (i = 0; i &lt; n; i++) {\r\nfor (j = 0; j &lt; n; j++) {\r\nif (i &gt; j) {\r\n}\r\n}\r\nprintf(\"%d\", matrix[i * SIZE + j]);\r\nprintf(\"\\n\");\r\n}\r\nint main() {\r\nint matrix[SIZE * SIZE]; int n, i, j;\r\nprintf(\"Enter the size of the matrix: \"); scanf(\"%d\", &amp;n);\r\nprintf(\"Enter the elements of the matrix:\\n\");\r\nfor (i =\r\n0; i &lt; n; i++) {\r\nfor (j = 0; j &lt; n; j++) {\r\nscanf(\"%d\", &amp;matrix[i * SIZE + j]);\r\n}\r\n}\r\n}\r\nfindDiagonal Triangles(matrix, n);\r\nreturn 0;\r\n<\/pre>\n<p>In this alternative implementation, we use a one-dimensional array (`matrix[SIZE * SIZE]`) to store the matrix elements. The elements are accessed using the formula `matrix[i * SIZE + j]`, where `i` and `j` represent the row and column indices, respectively. The rest of the code remains the same.<\/p>\n<p>These alternative implementations provide different approaches to solving the problem while achieving the same results. You can choose the one that suits your preferences or fits the requirements of your project.<\/p>\n<h3>Conclusion<\/h3>\n<p>In conclusion, we have discussed a C program that finds the diagonal upper and diagonal lower triangles of a given matrix. This program can be helpful in various applications involving matrix analysis and manipulation. Understanding these triangles and being able to extract their elements is essential in many algorithms and computations. Feel free to modify and extend this program based on your specific requirements or use it as a building block for more complex matrix operations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Matrices are fundamental mathematical structures used in various fields, including computer science and data analysis. They offer a convenient way to represent and manipulate data in a tabular format. One common task involving matrices&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":117868,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19488],"tags":[27998,27997],"class_list":["post-116563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-program-to-find-lower-triangular-matrix","tag-c-program-to-find-upper-triangular-matrix"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C Program to Find Diagonal Upper and Lower Triangle of Matrix - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn to write C program that finds diagonal upper &amp; diagonal lower triangles of a given matrix. It helps in matrix analysis &amp; manipulation.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C Program to Find Diagonal Upper and Lower Triangle of Matrix - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn to write C program that finds diagonal upper &amp; diagonal lower triangles of a given matrix. It helps in matrix analysis &amp; manipulation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-16T13:41:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-find-diagonal-upper-diagonal-lower-traingle-of-matrix.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C Program to Find Diagonal Upper and Lower Triangle of Matrix - DataFlair","description":"Learn to write C program that finds diagonal upper & diagonal lower triangles of a given matrix. It helps in matrix analysis & manipulation.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/","og_locale":"en_US","og_type":"article","og_title":"C Program to Find Diagonal Upper and Lower Triangle of Matrix - DataFlair","og_description":"Learn to write C program that finds diagonal upper & diagonal lower triangles of a given matrix. It helps in matrix analysis & manipulation.","og_url":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-08-16T13:41:43+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-find-diagonal-upper-diagonal-lower-traingle-of-matrix.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"C Program to Find Diagonal Upper and Lower Triangle of Matrix","datePublished":"2023-08-16T13:41:43+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/"},"wordCount":691,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-find-diagonal-upper-diagonal-lower-traingle-of-matrix.webp","keywords":["C program to find lower triangular matrix","C program to find upper triangular matrix"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/","url":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/","name":"C Program to Find Diagonal Upper and Lower Triangle of Matrix - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-find-diagonal-upper-diagonal-lower-traingle-of-matrix.webp","datePublished":"2023-08-16T13:41:43+00:00","description":"Learn to write C program that finds diagonal upper & diagonal lower triangles of a given matrix. It helps in matrix analysis & manipulation.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-find-diagonal-upper-diagonal-lower-traingle-of-matrix.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-find-diagonal-upper-diagonal-lower-traingle-of-matrix.webp","width":1200,"height":628,"caption":"cprogram to find diagonal upper & diagonal lower traingle of matrix"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-diagonal-upper-and-lower-triangle-of-matrix\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"C Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/c-programming\/"},{"@type":"ListItem","position":3,"name":"C Program to Find Diagonal Upper and Lower Triangle of Matrix"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=116563"}],"version-history":[{"count":7,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116563\/revisions"}],"predecessor-version":[{"id":118472,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116563\/revisions\/118472"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/117868"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=116563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=116563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=116563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}