Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Saturday, September 3, 2022

C program to find the largest and smallest using the array

C program to find the largest and smallest using the array


C program to find the largest and smallest using the array


Introduction:

In programming, arrays are commonly used to store and manipulate a collection of data. In this article, we will be discussing a C program that takes 'n' numbers as input and find the largest and smallest using the array. Then, the program finds the greatest and smallest number in the array and displays them to the user.

The program can be broken down into several steps:
  1. Declare an array to store 'n' numbers.
  2. Prompt the user to enter the value of 'n'.
  3. Use a for loop to take input of 'n' numbers from the user and store them in the array.
  4. Declare two variables 'max' and 'min' and initialize them with the first element of the array.
  5. Use a for loop to traverse the array and compare each element with 'max' and 'min'.
  6. If an element is greater than 'max', update the value of 'max'.
  7. If an element is smaller than 'min', update the value of 'min'.
  8. Display the values of 'max' and 'min' to the user.

Now let's look at the program in detail:


#include <stdio.h>
int main()
{
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int max = arr[0];
int min = arr[0];
for(i = 1; i < n; i++)
{
if(arr[i] > max)
{
max = arr[i];
}
if(arr[i] < min)
{
min = arr[i];
}
}
printf("Maximum element is: %d\n", max);
printf("Minimum element is: %d\n", min);
return 0;
}


Explanation:

In this program, we first declare variables 'n' and 'i' of type integer. 'n' is used to store the number of elements that the user wants to input, and 'i' is used as a counter in the for loop.

We then prompt the user to enter the value of 'n' using the printf() and scanf() functions. After that, we declare an array 'arr' of size 'n' to store the user input.

We then use a for loop to take input of 'n' numbers from the user and store them in the array.

Next, we declare two variables 'max' and 'min' and initialize them with the first element of the array 'arr[0]'.

We then use another for loop to traverse the array 'arr' and compare each element with 'max' and 'min'. If an element is greater than 'max', we update the value of 'max', and if an element is smaller than 'min', we update the value of 'min'.

Finally, we display the values of 'max' and 'min' to the user using printf() function.


Output:




Conclusion:

The above C program is a basic example of how arrays can be used to store and manipulate a collection of data. The program takes user input of 'n' numbers, stores them in an array, and then find the largest and smallest using the array. This program can be useful for beginners who want to learn more about arrays and for programmers who need to find the maximum and minimum values in an array.


You can also Read:





No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages