Code Scrappers


Smallest element in an array C program


              #include<stdio.h>
              #include<stdlib.h>

              void main()
              {
                    int arr[20];

                    int i, n, smallest;

                    printf("\nenter the no of elements : ");

                    scanf("%d", &n);

                    for (i = 0; i < n; i++)
                    {
                        scanf("%d", &arr[i]);
                    }

                    smallest = arr[0];

                    for (i = 0; i < n; i++)
                    {
                        if (arr[i] < smallest)
                        {
                            smallest = arr[i];
                        }
                    }

                    printf("\n%d is the smallest element", smallest);

                }

          



Output

        enter the no of elements : 5
        10
        21
        87
        2
        1

        1 is the smallest element