C Program to find the largest and smallest in one-dimensional Array. Also print its position.

 


#include<stdio.h>

main()

{

int a[100],max,min,pos1,pos2,n,i;

clrscr();

printf("\nEnter the total number of elements\n");

scanf("%d",&n);

printf("\nEnter the elements one by one: \n");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

max=a[0];

min=a[0];

pos1=pos2=0;

for(i=1;i<n;i++)

{

if(a[i]>max)

{

max=a[i];

pos1=i;

}

if(a[i]<min)

{

min=a[i];

pos2=i;

}

}

printf("\nLargest value is %d and found at %d Position\n",max,pos1+1);

printf("\nSmallest Value is %d and found at %d Position\n",min,pos2+1);

getch();

}


Output:

  1. Enter the total number of elements

4

Enter the elements one by one:

12

45

78

34

Largest value is 78 and found at 3 Position

Smallest Value is 12 and found at 1 Position



  1. Enter the total number of elements

3

Enter the elements one by one:

10

30

20

Largest value is 30 and found at 2 Position

Smallest Value is 10 and found at 1 Position



Post a Comment

Previous Post Next Post