CSIT C Programming (smallest and largest number stored in an array)

In this post, I am going to teach you how to find smallest and largest number stored in an array.

Question: Write a C Program to find largest and smallest number stored in array of size n.

Let's have a look at Coding. 

#include<stdio.h>
#include<conio.h>
int main()
{
    int n,i,j,greatest,smallest;
    printf("How many integer you are going to enter.\n");
    scanf("%d",&n);
    int a[n];
    printf("Enter %d Numbers.\n",n);
    for ( i = 0; i < n; i++)
    {
        scanf("%d",&a[i]);
    }
    //For greatest and smallest number in array
    greatest= smallest = a[0];
    for ( i = 0; i < n; i++)
    {
        if (a[i]>greatest)
        {
            greatest=a[i];
        }
        if (a[i]<smallest)
        {
            smallest=a[i];
        }
        
        
    }
    printf("THE GREATEST ENTRY IN ARRAY IS: %d\n",greatest); 
    printf("THE SMALLEST ENTRY IN ARRAY IS: %d\n",smallest);       
    return 0;   
}

If This post Helped you, Please leave a comment, and share with your Coder Friends.
Thank You.

Keep Learning, Keep Growing

Comments

Popular posts from this blog

CSIT Object Oriented Programming with C++(Unary Operator Overloading using Friend function) Part IV

CSIT Object Oriented Programming with C++(Insertion and Extraction Operator Overloading ) Part VI

CSIT Object Oriented Programming with C++(Unary Operator Overloading) Part III