C013 A C program that will take as input a set of integers and find and display the largest and the smallest values within the input data values

By | August 28, 2013

Let’s identify variables needed for this program.

In this program, we need several variables to store and compare the values to find the largest and the smallest out of them. Its not recommended to take number of variables declared of the same data type with different names and remembering it. It becomes worst when the variable numbers goes on increasing to large number. Hence we have a savior called ARRAY.

Array is a nothing different than any variable but the only difference is that it allows us to use more than one variables with a single name to it. The size of array is decided by us. We can take 2 to n numbers according to our requirement. It has to be accessed with the help of index number ranging from 0 to n-1. (e.g. num[0],num[1],num[2],num[3]) 

First variable will be the one which will save the values entered by the user and it will be NUM[10]. Here we are taking ten different values hence taking number 10. Second variable will be I which will be for FOR Loop. other variables will be holding largest and smallest numbers and it will be LARGE and SMALL so in all Four variables.
The identified variables are NUM,I,LARGE,SMALL.

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

In this program there is a requirement of accessing the array NUM[0],NUM[1]….NUM[9]. Here we have the index values ranging from zero to the given number minus one (i.e. n-1) . Best Loop for such kind of condition is FOR Loop the value of loop will start with 0 to 9. We will need two FOR loops, First one for scanning required array values and Second one for comparing all the other numbers to find largest and smallest out of them.

The variable for largest and smallest are already taken. But the problem is both are having no values inside it to compare so the logic is assign the first value to both LARGE and SMALL. So that we have the first value to compare and replace it with the other larger or smaller value with LARGE and SMALL respectively. Comparison is done with the help of IF condition below. FOR Loop the value of loop will start with 1 to 9. We will need this because First value is already assigned now we need to compare all the other numbers except the first to find largest and smallest out of them.

[codesyntax lang=”c”]

if(LARGE<NUM[I])
         LARGE=NUM[I];
 if(SMALL>NUM[I])
          SMALL=NUM[I];

[/codesyntax]

Finally print the Numbers as Largest and Smallest at the end of the program.

C program code :

[codesyntax lang=”c” lines=”normal”]

#include<stdio.h>
void main()
{
    int NUM[10],I,LARGE,SMALL;
    clrscr();
    printf("ENTER TEN NUMBERS TO COMPARE\n");
    for(I=0;I<10;I++)
    {
        printf("\nENTER NUMBER : ");
        scanf("%d",&NUM[I]);
    }
    LARGE=SMALL=NUM[0];
    for(I=1;I<10;I++)
    {
        if(LARGE<NUM[I])
            LARGE=NUM[I];
        if(SMALL>NUM[I])
            SMALL=NUM[I];
    }
    printf("\nTHE LARGER NUMBER IS %d", LARGE);
    printf("\nThe SMALLER NUMBER IS %d", SMALL);
    getch();
}

[/codesyntax]

SCREEN SHOTS:-

C_program_Large_Small

C_program_Large_Small_Output

 

Note:- To understand program for sequence in detail Please SEARCH numerically example: C001, C002, etc.

Leave a Reply