Contact Learn C
Copy Program Share Program

Program 104:To find minimum and maximum of given numbers

Program 104:
 
#include<stdio.h>
main()
{
    int i,size,max=0,min=0;
    printf("Enter size to find largest and smallest of given numbers\n");
    scanf("%d",&size);
    int a[size];
    printf("Enter numbers in array\n");
    for(i=0;i<size;i++)
    {
        scanf("%d",&a[i]);
    }
    min=a[0];
    max=a[0];
    for(i=0;i<size;i++)
    {
        if(a[i]>max)
        {
            max=a[i];
        }
        if(a[i]<min)
        {
            min=a[i];
        }
    }
    printf("The largest number is %d\n",max);
    printf("The smallest number is %d\n",min);
} 
Explanation:

  1. This program starts with initializing :
    • i→ Helping variable
    • size→Indicates size of array i.e number of elements to be stored in array
    • max,min→ To Store maximum and minimum numbers from given array
    • a[size]→ To store array elements of given size
  2.  printf("Enter size to find largest and smallest of given numbers\n");
        scanf("%d",&size);
        int a[size];
        printf("Enter numbers in array\n");
        for(i=0;i<size;i++)
        {
            scanf("%d",&a[i]);
        }
    Taking size and array elements as input from user
  3. min=a[0];
    max=a[0];
    Initializing min,max to first element in array.So lets take array elements say,45,30,35,32.
  4. Then min=45 and max=45
  5. for(i=0;i<size;i++)
    {
          if(a[i]>max)
          {
              max=a[i];
          }
          if(a[i]<min)
          {
              min=a[i];
          }
     }
    We are iterating from 1st element till last element in array.I will explain in brief since all of you by now are acquainted with for loop working.Since there are 4 elements in array 45,30,35,32.min=45,max=45. this is our intial data.
    • In 1st iteration where i=0
      • so a[0]=45>max which is not true since both are equal but not greater.so if part won't execute
      • a[0]<min=45<min is also false
      • Finally both if parts won't execute in 1st iteration
      • End of Iteration 1 : min=45,max=45 no change has been made
    • In 2nd iteration where i=1
      • so a[1]=30>max=30>45 which is not true so if part won't execute
      • a[1]<min=30<min =30<45 is true so if part will execute.Then min=a[2] --min=30
      • End of Iteration 2 : min=30,max=45
    • In 3rd iteration where i=2
      • so a[2]=35>max=35>45 which is not true so if part won't execute
      • a[2]<min=35<min =35<30 is not true so if part won't execute
      • End of Iteration 3 : min=30,max=45
    • In 4th iteration where i=3
      • so a[3]=32>max=32>45 which is not true so if part won't execute
      • a[3]<min=32<min =32<30 is not true so if part won't execute
      • End of Iteration 4 : min=30,max=45
  6. Finally min=30 and max=45 will be printed


 Output:

To find minimum and maximum of given numbers

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide