Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » C Programming » Programs » Arrays » C program to interchange the largest and smallest elements in the array
Suryateja Pericherla Categories: Arrays. No Comments on C program to interchange the largest and smallest elements in the array
3.8
(33)

In this article we will learn to implement a C program to interchange the largest and smallest elements in the array. A C program is provided below that reads a list of numbers and swap the largest and smallest element in the array.

 

Program is as follows:

//C program to interchange the largest and smallest elements in the array
#include<stdio.h>
#include<conio.h>
void main()
{
	int a[5],max,min,maxpos,minpos,i,temp;
	printf("Enter 5 integers: ");
	for(i=0;i<5;i++)
		scanf("%d",&a[i]);
	max=a[0];
	min=a[0];
	maxpos=0;
	minpos=0;
	for(i=1;i<5;i++)
	{
		if(a[i]>max)
		{
			max=a[i];
			maxpos=i;
		}
		if(a[i]<min)
		{
			min=a[i];
			minpos=i;
		}
	}
	temp=a[maxpos];
	a[maxpos]=a[minpos];
	a[minpos]=temp;
	printf("After interchange array elemnts are: ");
	for(i=0;i<5;i++)
		printf("%d ",a[i]);
	getch();
}

 

Input and output for the above program is as follows:

Enter 5 integers: 2 5 4 1 3
After interchange array elemnts are: 2 1 4 5 3

 

How useful was this post?

Click on a star to rate it!


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory