Code Scrappers


Largest element in an array C program


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

              void main()
              {
                    int arr[20];

                    int i, n, largest;

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

                    scanf("%d", &n);

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

                    largest = arr[0];

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

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


              }


          



Output

    Enter the no of elements :5
    4
    6
    2
    1
    8

    8 is the largest element