There was an error while loading. Please reload this page.
1 parent d09606c commit c177d4bCopy full SHA for c177d4b
Learning/7. Challenges/4. Arrays/smallest-array-element.c
@@ -0,0 +1,30 @@
1
+// Program to find the smallest element from an array.
2
+
3
+#include <stdio.h>
4
5
+int main() {
6
7
+ // create an integer array of size 5
8
+ int numbers[5];
9
10
+ // get input value for the array
11
+ for (int i = 0; i < 5; i++) {
12
+ scanf("%d", &numbers[i]);
13
+ }
14
15
+ // create a variable smallest and assign first element of numbers to it
16
+ int smallest = numbers[0];
17
18
+ // run a for loop from i = 1 to i < 5
19
+ // check if smallest is less than element at i
20
+ for (int i = 1; i < 5; i++) {
21
+ if (numbers[i] < smallest) {
22
+ smallest = numbers[i];
23
24
25
26
+ // print smallest
27
+ printf("%d", smallest);
28
29
+ return 0;
30
+}
0 commit comments