Courses

### Courses

Structure and Union

No Examples found for this topic - CodeHelpPro

Data Files

No Examples found for this topic - CodeHelpPro

# Program to find Largest and Smallest Number in an Array

0 Suresh Chand June 2, 2021

In this example, We will learn a program to find the largest and smallest number in an array. You must have knowledge of the following topics before looking at this example. This is the simple program where we will use for the loop.

## Algorithm

This is the algorithm of this program

```Step 1: Start
Step 2: Initialize variables a, size, i, big and small
Step 3: Take input from user and store in size
Step 4: Assign i = 0
Step 5: REPEAT this step until i < size
Take input from user and store in a[i]
Increase i by 1
Step 6: big = a[0]
Step 7: Assign i = 1
Step 8: REPEAT this step until i < size
IF big < a[i]
bid = a[i]
Increase i by 1
Step 9: Print big
Step 10: small = a[0]
Step 11: Assign i = 1
Step 12: REPEAT this step until i < size
IF small > a[i]
small = a[i]
Increase i by 1
Step 13: Print small
Step 14: End```

## Flowchart

This is the flowchart of this program

## CODE: Largest and Smallest Number in an Array

Now, We will see the code to find largest and smallest number in array using for loop.

``````#include <stdio.h>

int main()
{
int a[50], size, i, big, small;

printf("Enter the size of the array: ");
scanf("%d", &size);

printf("\nEnter the %d elements of the array: \n", size);
for (i = 0; i < size; i++)
scanf("%d", &a[i]);

//First we will asign first element as largest element
big = a[0];

for (i = 1; i < size; i++)
{
if (big < a[i]) // if larger value is found
{
big = a[i]; // update the value of big
}
}
printf("\nThe largest element is: %d", big);

//First we will asign first element as smallest element
small = a[0];
for (i = 1; i < size; i++)
{
if (small > a[i]) // if smaller value is found
{
small = a[i]; // update the value of small
}
}
printf("\nThe smallest element is: %d", small);
return 0;
}``````

The output of above program is

``````Enter the size of the array: 5

Enter the 5 elements of the array:
1
3
7
5
9

The largest element is: 9
The smallest element is: 1``````
Subscribe
Notify of
Inline Feedbacks