C Chapters

C Program that interchanges the odd and even elements of an array.

One Dimensional Arrays - Arrays
#include<stdio.h>
#include<conio.h>
int main()
{
	int a[] = {1,2,3,4,5,6},i,tmp;
	for (i = 0; i <= 5; i = i + 2)
	{
		tmp = a[i];
		a[i] = a[i + 1];
		a[i + 1] = tmp;
	}
	for (i = 0; i <= 5; i++)
	{
		printf("%d\n", a[i]);
	}
	getch();
	return 0;
}

Output :
2
1
4
3
6
5

Description :

Algorithm
Step - 1 : Declare an integer array with name a and initialize with n(Here n = 6) numbers & integer variable i and temp
Step - 2 : FOR i = 0 TO n-1 Increment by 2
Step - 3 : Read a[i] th value (Number at odd position) & store it in temp
Step - 4 : Read a[i+1] th value(Number at even position) & store it in a[i](Number at odd position)
Step - 5 : Read temp value & store it in a[i+1](Number at even position)
Step - 6 : END FOR
Step - 7 : FOR i = 0 TO n-1 Increment by 1
Step - 8 : Print a[i]
Step - 9 : END FOR


Leave a comment
Recent Comments
  • Shamiul Islam at 19-12-2017 04:02:26 PM
    please help me by giving the algorithm of this program
  • Shamiul Islam at 19-12-2017 04:02:21 PM
    please help me by giving the algorithm of this program