Write a program to find largest and smallest number in an array

Write a program to find largest and smallest number in an array


// Write a program to find largest and smallest number in an array
// Ex: [3,7,6,3,9,4,2] -> Largest:9, smallest:2

 
#include
int main(){
     int len,max,i=0,min;
     printf("Enter length of an array:");
     scanf("%d",&len);                 // array length input from user
     int array[len];
    
     // user input for array elements
     printf("Enter Matrix1 Elements");
     for(i=0;i<len;i++){
             scanf("%d",&array[i]);
     }
 
     // largest and smallest elements of array
     max = array[0];
     min = array[0];
     for(i=1;i<len;i++){ if(array[i]>max){
                    max = array[i];                
            }
            if(array[i]<min){
                    min = array[i];          
            }
     }
    
     // printing largest and smallest element
     printf("Largest Element of an Array: %d\n",max);
     printf("Smallest Element of an Array: %d",min);
}

No comments:

Post a Comment