Program to find maximum and minimum of entered ’n’ numbers using arrays.
With the help of program given below we can find the minimum and maximum number's in a given set of numbers.This can be clearly explained with an example
Example:
INPUT: 4 5 6 7 8 9 10
OUTPUT: Minimum number = 4
Maximum number = 10
Program:
/*C program to find max and min numbers */
| #include<stdio.h> #include<conio.h> main( ) { int i, n, a[10], minNum, maxNum; clrscr( ); //clears screen printf(“ Enter how many number you want :”); scanf(“%d”, &n); printf(“\nEnter the elements: ”); for(i=0;i<n;i++) scanf("%d", &a[i]); minNum = a[0]; for(i=0;i<n;i++) if(minNum > a[i]) minNum = a[i]; //finds min value printf(“\nMinimum number = %d”, minNum); maxNum=0; for(i=0;i<n;i++) if(maxNum < a[i]) maxNum = a[i]; //finds max value printf(“\nMaximum number = %d”, maxNum); getch( ); } |
Output:
| Enter how many number you want :6 Enter the elements: 1 2 3 4 5 6 Minimum number = 1 Maximum number = 6 |
Initially we include the header files required for the program.Here variables are declared as integer type by using keyword 'int',and clrscr() is used to clear the screen.The printf() statements are to print the given data on the monitor and scanf() statements are used to take or accept the inputs from the user.The 1st for loop is used to enter the elements given by the user(to decrease the code length we used for loop).Then we consider the 1st element entered is the minimum value and compare the value with next elements and replace if there is an any small number, hence we get the smallest number, in the same way we get the largest number too.

0 Comments