C program to find the min and max of given numbers without using array
kw.c
#include <stdio.h>
int main()
{
    int i, n, num, min, max = 0;
    printf("———————————————————————————————————————————");
    printf("\nProgram to find the min/max of given numbers");
    printf("\n———————————————————————————————————————————");
    printf("\nHow many numbers you want to enter ");
    scanf("%d", &n);
    printf("\nEnter %d numbers\n", n);
    scanf("%d", &num);
    min = num;
    if (num >= max)
        max = num;
    for (i = 1; i < n; i++)
    {
        scanf("%d", &num);
        if (num > max)
            max = num;
        if (num < min)
            min = num;
    }
    printf("\nMaximum number %d", max);
    printf("\nMinimum number %d", min);
    printf("\n———————————————————————————————————————————\n");
    return 0;
}
Output
kodingwindow@kw:~$ gcc kw.c
kodingwindow@kw:~$ ./a.out ——————————————————————————————————————————— Program to find the min/max of given numbers ——————————————————————————————————————————— How many numbers you want to enter 5 Enter 5 numbers -10 50 1005 2048 155 Maximum number 2048 Minimum number -10 ——————————————————————————————————————————— kodingwindow@kw:~$
C program to find the min and max of given numbers using array
kw.c
#include <stdio.h>
#define N 10
int main()
{
    int a[N], max, min;
    printf("———————————————————————————————————————————");
    printf("\nProgram to find the min/max of given numbers");
    printf("\n———————————————————————————————————————————");
    printf("\nEnter %d numbers\n", N);
    for (int i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
    }
    max = a[0];
    min = max;
    for (int i = 0; i < N; i++)
    {
        if (a[i] > max)
            max = a[i];
        if (a[i] < min)
            min = a[i];
    }
    printf("\nMaximum number %d", max);
    printf("\nMinimum number %d", min);
    printf("\n———————————————————————————————————————————\n");
    return 0;
}
Output
kodingwindow@kw:~$ gcc kw.c
kodingwindow@kw:~$ ./a.out ——————————————————————————————————————————— Program to find the min/max of given numbers ——————————————————————————————————————————— Enter 10 numbers 5 -25 -5005 1024 2048 155 233 65535 625 1225 Maximum number 65535 Minimum number -5005 ——————————————————————————————————————————— kodingwindow@kw:~$
Advertisement