

{"id":116578,"date":"2023-08-22T19:15:11","date_gmt":"2023-08-22T13:45:11","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=116578"},"modified":"2023-08-22T19:15:56","modified_gmt":"2023-08-22T13:45:56","slug":"c-program-to-find-largest-element-in-an-array","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/","title":{"rendered":"C Program to Find Largest Element in an Array"},"content":{"rendered":"<p>When working with arrays in programming, it is often necessary to find the largest element within them. This task can be achieved through various approaches and algorithms. In this article, we will delve into different implementations of a C program that locates the largest element in an array, providing you with multiple options to accomplish this common task efficiently.<\/p>\n<h3>Implementation of C Program to Find Largest Element in an Array<\/h3>\n<p><strong>Method 1: Linear Search to find Largest Element in array in C<\/strong><\/p>\n<p>The simplest and most straightforward method to find the largest element in an array is by performing a linear search. This approach involves iterating through the array, comparing each element with a temporary variable that holds the current maximum value. Here&#8217;s the code for the linear search implementation:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\nint findLargestElement(int arr[], int n) \r\n{ \r\nint max = arr[0];\r\nfor (int i = 1; i &lt; n; i++) {\r\nif (arr[i] &gt; max) {\r\nmax = arr[i];\r\n}\r\nreturn max;\r\n}\r\nint main() {\r\nint arr[] = {10, 45, 98, 76, 33};\r\nint size = sizeof(arr) \/ sizeof(arr[0]);\r\nint largest = findLargestElement (arr, size);\r\nprintf(\"The largest element in the array is: %d\\n\", largest);\r\nreturn 0;\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">The largest element in the array is: 98<\/div>\n<p><strong>Method 2: Sorting the Array to find largest element in an array using C<\/strong><\/p>\n<p>Another approach to finding the largest element involves sorting the array in ascending order and returning the last element. While this method is conceptually simple, it may not be the most efficient for large arrays. Here&#8217;s the code for this implementation:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\nvoid bubbleSort(int arr[], int n) \r\n{ \r\nfor (int i = 0; i &lt; n - 1; i++) {\r\nfor (int j = 0; j &lt; n - i - 1; j++) {\r\nif (arr[j] &gt; arr[j + 1]) {\r\nint temp = arr[j];\r\narr[j] = arr[j + 1];\r\narr[j + 1] = temp;\r\n}}}}\r\n\r\nint findLargestElement(int arr[], int n) \r\n{ \r\nbubbleSort (arr, n);\r\nreturn arr[n - 1];\r\n}\r\n\r\nint main() {\r\nint arr[] = {10, 45, 98, 76, 33};\r\nint size = sizeof(arr) \/ sizeof(arr[0]);\r\nint largest = findLargestElement (arr, size);\r\nprintf(\"The largest element in the array is: %d\\n\", largest);\r\nreturn 0;\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">The largest element in the array is: 98<\/div>\n<p><strong>Method 3: Using Divide and Conquer (Recursive) to find largest element in an array<\/strong><\/p>\n<p>The divide and conquer strategy can also be employed to find the largest element in an array. This recursive approach divides the array into smaller subarrays until a single element remains, which is then returned as the largest element. Here&#8217;s the code for this recursive implementation:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\nint findLargestElementRecursive(int arr[], int low, int high) { \r\nif (low == high) {\r\nreturn arr[low];\r\n} else {\r\nint mid = (low + high) \/ 2;\r\nint leftMax = findLargestElementRecursive(arr, low, mid);\r\nint rightMax = findLargestElementRecursive(arr, mid + 1, high); \r\nreturn (leftMax &gt; rightMax) ? leftMax: rightMax;\r\n}\r\n}\r\nint main() {\r\nint arr[] = {10, 45, 98, 76, 33};\r\nint size = sizeof(arr) \/ sizeof(arr[0]);\r\nint largest = findLargestElementRecursive(arr, 0, size - 1); printf(\"The largest element in the array is: %d\\n\", largest); return 0;\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">The largest element in the array is: 98<\/div>\n<h3>Conclusion:<\/h3>\n<p>In this article, we explored three different implementations of a C program to find the largest element in an array. The linear search method provides a simple and efficient solution for most scenarios. However, sorting the array and applying divide and conquer are alternative approaches that may be useful in specific situations. Remember to choose the most suitable method based on the size of the array and the performance requirements of your program.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with arrays in programming, it is often necessary to find the largest element within them. This task can be achieved through various approaches and algorithms. In this article, we will delve into&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":117876,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19488],"tags":[28007],"class_list":["post-116578","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-program-to-find-largest-element-in-an-array"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C Program to Find Largest Element in an Array - DataFlair<\/title>\n<meta name=\"description\" content=\"Explore three implementations of a C program to find the largest element in an array like linear search, sorting, Divide and Conquer.\" \/>\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-largest-element-in-an-array\/\" \/>\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 Largest Element in an Array - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Explore three implementations of a C program to find the largest element in an array like linear search, sorting, Divide and Conquer.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/\" \/>\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-22T13:45:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-22T13:45:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-find-largest-element-in-an-array.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=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C Program to Find Largest Element in an Array - DataFlair","description":"Explore three implementations of a C program to find the largest element in an array like linear search, sorting, Divide and Conquer.","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-largest-element-in-an-array\/","og_locale":"en_US","og_type":"article","og_title":"C Program to Find Largest Element in an Array - DataFlair","og_description":"Explore three implementations of a C program to find the largest element in an array like linear search, sorting, Divide and Conquer.","og_url":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-08-22T13:45:11+00:00","article_modified_time":"2023-08-22T13:45:56+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-largest-element-in-an-array.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":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"C Program to Find Largest Element in an Array","datePublished":"2023-08-22T13:45:11+00:00","dateModified":"2023-08-22T13:45:56+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/"},"wordCount":352,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-find-largest-element-in-an-array.webp","keywords":["C Program to Find Largest Element in an Array"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/","url":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/","name":"C Program to Find Largest Element in an Array - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-find-largest-element-in-an-array.webp","datePublished":"2023-08-22T13:45:11+00:00","dateModified":"2023-08-22T13:45:56+00:00","description":"Explore three implementations of a C program to find the largest element in an array like linear search, sorting, Divide and Conquer.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-find-largest-element-in-an-array.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-find-largest-element-in-an-array.webp","width":1200,"height":628,"caption":"c program to find largest element in an array"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-largest-element-in-an-array\/#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 Largest Element in an Array"}]},{"@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\/116578","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=116578"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116578\/revisions"}],"predecessor-version":[{"id":118655,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116578\/revisions\/118655"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/117876"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=116578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=116578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=116578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}