Swap even number with odd number and vice-versa: C Program

0
4619

swap even odd number

C program to swap even position numbers with odd position numbers and vice-versa

This C program swap or vice-versa even and odd numbers.For example consider i.e. m[0]=1, m[1]=2  after swapping we get m[0]=2 and m[1]=1.

 

[nextpage title=”PROGRAM” ]

[message_box title=”PROGRAM” color=”yellow”]

/* Written by Utpal Chaudhary  */

/* Student of L.D COLLEGE OF ENGINEERING, Ahmedabad-15, Gujarat */

#include <stdio.h>
#include <conio.h>

void main()

{

int m[20], i, n, temp, limit;

clrscr();

printf (” How many elements ? \n”);

scanf (“%d”, n );

printf (” Enter % d  elements below \n”,n);

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

{

scanf ( “%d”, &m[i] );

}

if ( n%2==0 )

{

limit =n;

}

else

{

limit =n-1;

}

for( i=0; i<limit; i+=2 )

{

temp=m[i];

m[i]=m[i+1];

m[i+1]=temp;

}

printf ( “After Swapping \n” );

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

{

printf( ” %d \n  “, m[i] );

}

}

[/message_box]

[/nextpage]

[nextpage title=”OUTPUT” ]

[message_box title=”OUTPUT” color=”yellow”]

How many elements?

6

Enter 6 elements below

1

2

3

4

5

6

After Swapping

2

1

4

3

6

5

[/message_box]
[/nextpage]

/* Written by Utpal Chaudhary  */

/* Student of L.D COLLEGE OF ENGINEERING, Ahmedabad-15, Gujarat */

/* Written by Utpal Chaudhary  */

/* Student of L.D COLLEGE OF ENGINEERING, Ahmedabad-15, Gujarat */

/* Written by Utpal Chaudhary  */

/* Student of L.D COLLEGE OF ENGINEERING, Ahmedabad-15, Gujarat */

/* Written by Utpal Chaudhary  */

/* Student of L.D COLLEGE OF ENGINEERING, Ahmedabad-15, Gujarat */

/* Written by Utpal Chaudhary  */

/* Student of L.D COLLEGE OF ENGINEERING, Ahmedabad-15, Gujarat */

/* Written by Utpal Chaudhary  */

/* Student of L.D COLLEGE OF ENGINEERING, Ahmedabad-15, Gujarat */

/* Written by Utpal Chaudhary  */

/* Student of L.D COLLEGE OF ENGINEERING, Ahmedabad-15, Gujarat */

/* Written by Utpal Chaudhary  */

/* Student of L.D COLLEGE OF ENGINEERING, Ahmedabad-15, Gujarat */

/* Written by Utpal Chaudhary  */

/* Student of L.D COLLEGE OF ENGINEERING, Ahmedabad-15, Gujarat */

/* Written by Utpal Chaudhary  */

/* Student of L.D COLLEGE OF ENGINEERING, Ahmedabad-15, Gujarat */