Max of an array

mail@pastecode.io avatar
unknown
c_cpp
22 days ago
659 B
5
Indexable
Never
// write a program to find max of an array

#include <stdio.h>

int main()
{
    int n, i;
    printf("Enter size of an array\n");
    scanf("%d", &n);
    int a[n];
    printf("Enter array elements \n");
    for(i =0; i<n; i++)
    {
        printf("Enter %d element: ", i+1);
        scanf("%d", &a[i]);
    }
    int max = a[0];
    for(i=0; i<n; i++) {
        if(max < a[i])
        {
            max = a[i];
        } else {
            continue;
        }
    }
    printf("Max is %d", max);
    return 0;
}

/* 
Enter size of an array
4
Enter array elements 
Enter 1 element: 24
Enter 2 element: 5
Enter 3 element: 66
Enter 4 element: 7
Max is 66
*/

Leave a Comment