C Program to Find Minimum and Maximum Elements in a 1D Array (1D Array में न्यूनतम और अधिकतम तत्व ज्ञात करने का C प्रोग्राम)
In C programming, an array is used to store multiple values of the same data type. We can find the minimum (smallest) and maximum (largest) elements of an array by comparing its elements.
C प्रोग्रामिंग में Array का उपयोग एक ही प्रकार के कई मानों को संग्रहीत करने के लिए किया जाता है। इस प्रोग्राम में हम Array के सभी तत्वों की तुलना करके सबसे छोटा (Minimum) और सबसे बड़ा (Maximum) तत्व ज्ञात करेंगे।
C Program
#include <stdio.h>#define N 5int main(){int list[N], i, min, max;printf("Enter %d elements:\n", N);for (i = 0; i < N; i++){printf("Enter element %d: ", i + 1);scanf("%d", &list[i]);}min = max = list[0];for (i = 1; i < N; i++){if (list[i] > max)max = list[i];if (list[i] < min)min = list[i];}printf("Minimum Element = %d\n", min);printf("Maximum Element = %d\n", max);return 0;}
Output (आउटपुट)
Enter 5 elements:Enter element 1: 12Enter element 2: 5Enter element 3: 28Enter element 4: 3Enter element 5: 16Minimum Element = 3Maximum Element = 28
Explanation (व्याख्या)
min = max = list[0];initializes both minimum and maximum with the first array element.The
forloop compares each remaining element withminandmax.If an element is smaller than
min, it updates the minimum.If an element is greater than
max, it updates the maximum.
सबसे पहले Array के पहले तत्व को Minimum और Maximum दोनों मान लिया जाता है। फिर for लूप द्वारा बाकी सभी तत्वों की तुलना की जाती है। यदि कोई तत्व Minimum से छोटा होता है, तो Minimum बदल दिया जाता है। इसी प्रकार, यदि कोई तत्व Maximum से बड़ा होता है, तो Maximum बदल दिया जाता है। यह प्रोग्राम लूप और तुलना की सहायता से 1D Array में सबसे छोटा और सबसे बड़ा तत्व ज्ञात करता है।
Comments
Post a Comment