Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
110 views

Program To Find Smallest Element in An Array Using C

The document discusses C programs to find the smallest and largest elements in an array. It explains how to initialize variables, take input from the user, and use a for loop to compare each element to the current smallest/largest. The smallest is initially the first element, and it is compared to other elements, updating if a smaller one is found. Similarly for largest element. Code examples with explanations are provided to find the smallest element, largest element, and both together in one program.

Uploaded by

AJAY J BIT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Program To Find Smallest Element in An Array Using C

The document discusses C programs to find the smallest and largest elements in an array. It explains how to initialize variables, take input from the user, and use a for loop to compare each element to the current smallest/largest. The smallest is initially the first element, and it is compared to other elements, updating if a smaller one is found. Similarly for largest element. Code examples with explanations are provided to find the smallest element, largest element, and both together in one program.

Uploaded by

AJAY J BIT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

(https://prepinsta.

com)

(https://prepinsta.com) (https://prepinsta.com/interview-experience/all) (https://prepinsta.com/feed) () ()

Program to find Smallest element in an array using C


June 24, 2020

Smallest Element in an array


Today we will find smallest or minimum element in an array using C concept.We apply algorithm that assumes the first
element as smallest element and then compare it with other elements if an element is smaller than it then, it becomes the
new smallest element, and this process is repeated till complete array is scanned.
For Example:- Input:- {5,2,9,15,6},input from the user
Output:- {2},smallest element in an array
Here we have given a solution of this query in C Programming.


Show More
Working:-
We can find smallest element in an array by following these steps:-

1. Initialize the required variables.


2. Take the input from user.
3. Assume smallest element is present at first position.
4. Scan each element through a for loop.
5. Check if element Scanned is new smaller than small.
6. If it is smallest than small, change the value of small with that element.
7. Print small as it stores the smallest element of array.

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

Please Login/Signup to comment

Support Companies All Exams Get In Get In Touch


Dashboards Touch
Contact Us Accenture Microsoft
(https://prepinsta.com/contact/)
(https://prepinsta.com/how- CoCubes Dashboard
(https://prepinsta.com/microsoft/) facebook support@prepinsta.com
About Us to-clear- TCS (https://prepinsta.com/cocubes/)
(https://www.facebook.com/prepinsta/)
(mailtp:support@prepinsta
(https://prepinsta.com/about/) eLitmus Dashboard
accenture- (https://prepinsta.com/how- Twitter +91-
Refund Policy written- to-prepare- (https://prepinsta.com/how-
(https://twitter.com/prepinsta)
8448440710
(https://prepinsta.com/refund-
test/) for-tcs- to-prepare-for- G+ (tel:918448440710)
policy/) Cognizant written- elitmus-written-test/) (https://plus.google.com/+PrepInsta)
Text Us on
Show More
(https://prepinsta.com)

(https://prepinsta.com) (https://prepinsta.com/interview-experience/all) (https://prepinsta.com/feed) () ()

C Program to find largest element in an array


June 24, 2020

Largest element in an array


Today, we will learn how to find the largest element present in an array. We will do this by first taking the value of the first
element in the variable largest_ele. Then we will compare with remaining elements of the array and store the value if
another larger number is found in this array. This will go on array_size-1 times and the program ends.


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)

(https://prepinsta.com) (https://prepinsta.com/interview-experience/all) (https://prepinsta.com/feed) () ()

C Program to find Largest and Smallest Element in an Array


June 24, 2020

Largest and Smallest element in an array


Here we will discuss how to find the smallest element and largest element from inputted One Dimensional Array Elements.
In this array we traverse elements recursively and encounter the value of smallest element and largest element until the
end of the array with the help of concept of C and For loop in the code.
The solution of this problem is given in C programming language.


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;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the %d elements of the array:\n", size);


for(i = 0; i < size; i++)
scanf("%d", &a[i]);

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)

(https://prepinsta.com) (https://prepinsta.com/interview-experience/all) (https://prepinsta.com/feed) () ()

C Program to find Sum of Elements in an Array


June 24, 2020

Sum of Elements in an Array


Today we will learn how to find sum of elements in an array is easy task when you know how to iterate through array
elements. In this problem we will explain you C approach to find sum of array elements inputted by the user.
Here is the solution of this query in C Language.


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);

printf("Enter %d elements in the array: ",m);// Input elements in array


for(i=0; i<m; i++)
{
scanf("%d", &arr[i]);
}
for(i=0; i<m; i++) //Add each elements to sum array
{
sum = sum + arr[i];
}
printf("Sum of elements in an array = %d", sum);
return 0;
}

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)

(https://prepinsta.com) (https://prepinsta.com/interview-experience/all) (https://prepinsta.com/feed) () ()

C Program to Reverse elements of an array


June 24, 2020

Learn how to reverse an array


Today, we will learn how to print the elements of the array in reverse order.That is:- The last element should be displayed
first, followed by second last element and so on.
For Example:-
arr{1,2,3,4,5} is the array input by the user and the reverse order of it should be arr{5,4,3,2,1}

To answer this problem here in C programming language.


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);

printf("Enter the elements of an array:\n"); //print elements of an array


for (p = 0; p < n ; p++)
scanf("%d", &a[p]);

for (p = n - 1, p = 0; p >= 0; p--, q++)


b[q] = a[p];

for (p = 0; p < n; p++) //Reverse of digit


a[p] = b[p];
printf("Reverse of an array is:\n");

for (p = 0; p < n; p++)


printf("%d\n", a[p]); //print Reverse of an array

return 0;
}

Output:Enter the number of elements in an array:


5
Enter the elements of an array:
12
23
30
46
55
Reverse of an array is:
55
46
30
23
12

Show More
(https://prepinsta.com)

(https://prepinsta.com) (https://prepinsta.com/interview-experience/all) (https://prepinsta.com/feed) () ()

C program to sort an array in ascending and descending


order
June 24, 2020

Sorting of array in ascending and descending order


Today we will learn how to sort an array in ascending and descending manner.There are numerous logic to sort an array
elements. Here we will using general algorithm, we select an element from an array and place it to its correct position by
comparing with subsequent elements.
For Example:
Input :- From the user {4,3,6,1,8}
Output :- In ascending order {1,3,4,6,8} and In descending order {8,6,4,3,1}.


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]) ;
}

for(p=0;p<n;p++) // loop for sorting array in descending order


{
for(q=p+1;q<n;q++)
{
if(a[p]<a[q])
{
temp1=a[p];
a[p]=a[q];
a[q]=temp1;
}
}
}
printf("\n Descending order of an array : \n"); // print descending order
for(p=0;p<n;p++)
{
printf("%d ",a[p]) ;
}
}

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

Please Login/Signup to comment

Show More

You might also like