Program To Find Smallest Element in An Array Using C
Program To Find Smallest Element in An Array Using C
com)
Show More
Working:-
We can find smallest element in an array by following these steps:-
Show More
C Program:-
//C Code to find smallest element in an array
#include<stdio.h>
int main()
{
int a[50], p, num, smallest;
printf("Enter the size of array :"); // Take size of array
scanf("%d", &num);
printf("Enter the elements of array : \n");
for (p = 0; p < num; p++) //Read elements of an array
scanf("%d", &a[p]);
smallest = a[0]; //Let first element as smallest
for (p = 0; p < num; p++)
{
if (a[p] < smallest)
{
smallest = a[p];
}
}
printf("Smallest Element of an array : %d", smallest); // Print smallest element
return (0);
}
Output:-
Enter the size of array : 6
Enter the elements of array :
2
0
8
44
6
17
Smallest Element of an array : 0
Show More
Working:-
1. Take the size of the array from the user as a input.
2. Then initialize an array of size provided by the user.
3. Using for loop take array element as input from users and insert them into the array.
4. After inserting all the elements of the array consider the very first element of array to be the largest.
5. Run a for loop from 1 to array_size-1, extracting array element one by one and comparing it to the element.
6. If the largest element is smaller than the element being compared, then the largest element is updated with the value of the
current element of the array.
7. In the end, the largest element will take the actual largest value present in the array.
Show More
C Program:-
#include <stdio.h>
int main()
{
int size, i, largest_ele;
printf("\n Enter the size of the array: "); //taking size of array
scanf("%d", &size);
int array[size];
printf("\n Enter %d elements of the array: \n", size); //print size and take input from user
for (i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
largest_ele = array[0];
for (i = 1; i < size; i++) //loop for checking largest element
{
if (largest_ele < array[i])
largest_ele = array[i];
}
printf("\n largest element in array is : %d", largest_ele); //print largest element in array
return 0;
}
Output:
Enter the size of the array : 5
Enter 5 elements of the array:
52
24
12
36
95
largest element in array is : 95
Show More
(https://prepinsta.com)
Show More
Implementation:-
Take the size of array from the user
Input the array elements from the user
Initialize small = large = arr[0]
Repeat from i = 1 to size of array
if(arr[i] > large)
large = arr[i]
if(arr[i] < small)
small = arr[i]
Print the element as smallest and largest
Show More
C Code:-
//C Program to Find Largest and Smallest Element in an Array
#include <stdio.h>
int main()
{
int a[50], size, i, large, small;
large = a[0];
for(i = 1; i < size; i++)
{
if(large < a[i]) // if larger value is encountered
{
large = a[i]; // update the value of large
}
}
printf("The largest element is: %d",large);
small = a[0];
for(i = 1; i < size; i++) { if(small>a[i]) // if smaller value is encountered
{
small = a[i]; // update the value of small
}
}
printf("\nThe smallest element is: %d", small);
return 0;
}
Output:
Enter the size of the array: 6
Enter the 6 elements of the array:
9
32
14
75
100
55
The largest element is: 100
The smallest element is: 9
Show More
(https://prepinsta.com)
Show More
Working:-
1. Input the size of array and store the value in m and arr[m].
2. To store sum of array elements, initialize a variable sum = 0.
3. To find sum of all elements, iterate through each element and add the current element to the sum.
4. Inside the loop add the current array element to sum i.e.sum = sum + arr[i] or sum += arr[i].
Show More
C Program:-
//C Program to find Sum of Elements in an Array
#include
#define ARR_SIZE 100
int main()
{
int arr[ARR_SIZE];
int i,m,size,sum=0;
printf("Enter size of the array: ");// Input size of the array
scanf("%d", &m);
Output:
Enter size of the array: 5
Enter 5 elements in the array: 5
5
5
5
5
Sum of elements in an array = 25
Show More
(https://prepinsta.com)
Show More
Algorithm:-
STEP 1: START
STEP 2: INITIALIZE a[100] and b[100]
STEP 3: Enter size of an array.
STEP 4: PRINT “Array Elements:”
STEP 5: REPEAT STEP 6 and STEP 7 UNTIL i=0
STEP 6: PRINT arr[p]
STEP 7: p=p+1
STEP 8: PRINT new line
STEP 9: PRINT “Array in reverse order”
STEP 10: SET p=length-1. REPEAT STEP 11 and STEP 12 UNTIL p>=0
STEP 11: PRINT a[p]
STEP 12: p=p-1
STEP 13: RETURN 0
STEP 14: END
Show More
C Program:-
// C code to Reverse of an array
#include
int main()
{
int n, p, q, a[100], b[100];
printf("Enter the number of elements in an array:\n"); //input size of array
scanf("%d", &n);
return 0;
}
Show More
(https://prepinsta.com)
Implementation:- Show More
Input size of array.
Store it in some variable say n and a[].
To select each element from array, run an outer loop from 0 to n-1.
Run another inner loop from i + 1 to n – 1 to place newly selected element at its correct position.
Inside inner loop to compare currently selected element with subsequent element and swap two array elements, if not placed
at its correct position.
Show More
C Code:-
//C Program to sort an array in ascending or descending order
#include <stdio.h>
void main()
{
int i,j,n,a[100],temp,p,q,temp1;
printf("Enter the size of array : \n") ; //Taking size of array
scanf("%d",&n) ;
printf("Enter the elements : \n") ; //Taking input from user
for(i=0;i<n;i++)
{
scanf("%d",&a[i]) ;
}
for(i=0;i<n;i++) // loop for sorting array in ascending order
{
for(j=i+1;j<n;j++) { if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Ascending order of an array : \n"); //print ascending order
for(i=0;i<n;i++)
{
printf("%d ",a[i]) ;
}
Output:-
Enter the size of array :
5
Enter the elements :
9
5
1
2
7
Ascending order of an array :
1 2 5 7 9
Descending order of an array :
9 7 5 2 1
Show More