Find largest and smallest element of array using pointer - C

(b)WAP to find smallest and largest numbers in a given array using pointer.
==> Code goes like this :
#include <stdio.h>

int main() {
    int a[10],size,*p,max,min,i;
    printf("\n Enter array size: ");
    scanf("%d",&size);
    printf("\n Enter %d element of array: ",size);
    for (int i = 0; i <size ; ++i) {
        scanf("%d",&a[i]);
    }
    p=a;
    max=a[0],min=a[0];
    for (int i = 0; i < size; ++i) {
        if (*p>max)
        {
            max=*p;
        }
        if (*p<min)
        {
            min=*p;
        }
        p++;
    }
    printf("\nLargest number =%d\t smallest number= %d",max,min);
    return 0;
}

Output:






















If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment