C Language | Arrays 7

Question:13
C program to take input for 5 elements using an array, check and print the largest element.
Sol:

#include<stdio.h>

int main()
{
    int a[5],i,m;
    float av;
    /* input */
    for(i=0;i<5;i++)
    {
        printf("Enter %d element ",i);
        scanf("%d",&a[i]);
    }
    /* display */
    m=a[0];
    for(i=0;i<5;i++)
    {
        printf("%d\n",a[i]);
        if(a[i]>m)
            m=a[i];
    }
    printf("Largest element =  %d\n",m);
    return(0);
}
/* Output */

Enter 0 element 25
Enter 1 element 63
Enter 2 element 5
Enter 3 element 95
Enter 4 element 32
25
63
5
95
32
Largest element =  95

Question:14
C program to take input for 5 elements using an array, check and print the lowest element.
Sol:

#include<stdio.h>

int main()
{
    int a[5],i,m;
    float av;
    /* input */
    for(i=0;i<5;i++)
    {
        printf("Enter %d element ",i);
        scanf("%d",&a[i]);
    }
    /* display */
    m=a[0];
    for(i=0;i<5;i++)
    {
        printf("%d\n",a[i]);
        if(a[i]<m)
            m=a[i];
    }
    printf("Lowest element =  %d\n",m);
    return(0);
}
/* Output */

Enter 0 element 25
Enter 1 element 69
Enter 2 element 3
Enter 3 element 54
Enter 4 element 85
25
69
3
54
85
Lowest element =  3

Question:15
C program to take input for 5 elements using an array, check and print the following:
* largest element
* lowest element
* diff between the largest and lowest element
Sol:

#include<stdio.h>

int main()
{
    int a[5],i,ma,mi,d;
    float av;
    /* input */
    for(i=0;i<5;i++)
    {
        printf("Enter %d element ",i);
        scanf("%d",&a[i]);
    }
    /* display */
    ma=a[0];
    mi=a[0];
    for(i=0;i<5;i++)
    {
        printf("%d\n",a[i]);
        /* max no */
        if(a[i]>ma)
            ma=a[i];
        /* min no */
        if(a[i]<mi)
            mi=a[i];
    }
    printf("Largest element = %d\n",ma);
    printf("Lowest element =  %d\n",mi);
    d=ma-mi;
    printf("Difference between largest and lowest element = %d\n",d);

    return(0);
}
/* Output */

Enter 0 element 25
Enter 1 element 3
Enter 2 element 96
Enter 3 element 7
Enter 4 element 5
25
3
96
7
5
Largest element = 96
Lowest element =  3
Difference between largest and lowest element = 93

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects