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

0
26

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.

 

[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]

/* 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 */

 

Leave a Reply

Be the First to Comment!

Notify of
wpDiscuz