C programming Minimum and Maximum of Numbers

It has two C programming code to find minimum value and maximum value of numbers, they are

  1. Minimum of Numbers - The input of the program is N numbers and the output is minimum of given inputs
  2. Maximum of Numbers - The input of the program is N numbers and the output is maximum of given inputs

Minimum of array of numbers - Algorithm

          set min=INT_MAX
          Read N integers
          For i=1 : N

             IF   integers[i] < min  then
                   min =integers[i]  
             End IF 

          End For 
          print min
   

Maximum of array of numbers - Algorithm

          set max=0
          Read N integers
          For i=1 : N

             IF   integers[i] > max  then
                   max =integers[i]  
             End IF 

          End For 
          print max
   

Minimum of Numbers - C programming code

The C program reads N integers from user while the program is running, and prints the result minimum value of given integers

 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {


   int N; 
   printf ("\n Find minimum of N numbers ");
   
   printf("\n Enter a  value for N:" );
   scanf("%d", &N);
   
   int *val=(int*) malloc(sizeof(int)*N);
   int min=INT_MAX;   
   for( int i=0;i< N ; i++ ) {
   
     printf("\n Enter %dth  value :" ,i);
     scanf("%d", &val[i]);
      min = ( val[i] < min )  ? val[i] : min;
   }
   
   printf("\nThe minimum of N numbers is = %d",min);    
       
   return 0;
}

 

Minimum of Numbers - C program output


 Find minimum of N numbers

 Enter a  value for N:5

 Enter 0th  value :6
 Enter 1th  value :7
 Enter 2th  value :8
 Enter 3th  value :4
 Enter 4th  value :12

The minimum of N numbers is = 4




Maximum of Numbers - C programming code

The C program reads N integers from user while the program is running, and prints the result maximum value of given integers

 

#include <stdio.h>
#include <stdlib.h>

int main() {


   int N; 
   printf ("\n Find maximum of N numbers ");
   
   printf("\n Enter value for N:" );
   scanf("%d", &N);
   
   int *val=(int*) malloc(sizeof(int)*N);
   int max=0;   
   for( int i=0;i< N ; i++ ) {
   
     printf("\n Enter %dth  value :" ,i );
     scanf("%d", &val[i]);
      max = ( val[i] > max )  ? val[i] : max;
   }
   
   printf("\nThe maximum of N numbers is = %d",max);    
       
   return 0;
}

 

Maximum of Numbers - C program Output


Find maximum of N numbers
Enter value for N:5

Enter 0th  value :6
Enter 1th  value :8
Enter 2th  value :3
Enter 3th  value :9
Enter 4th  value :5

he maximum of N numbers is = 9



Download c program source code

Comments

Popular posts from this blog

Convert Octal to Binary

Hexadecimal to Binary Conversion

C program Simple Interest and Compound Interest