Write a 'C' program to swap the alternate digits of the given number Input Format input will have an integer Constraints 1 ≤ num ≤ 100000000 Output Format Print the value Sample Input 0 54687 Sample Output 0 45867
Answers
Answer:
#include <stdio.h>
int main ( )
{
int a [ 100000000 ], n, i ;
int t ;
printf ( " Enter total number of elements: " );
scanf ( " % d " ,& n ) ;
if ( n % 2 != 0 )
{
printf ( " Total number of elements should be EVEN. " ) ;
return 1;
}
// read array elements
printf ( " Enter array elements:\n");
for( i = 0 ; i < n ; i++)
{
printf ( " Enter element % d " , i + 1 );
scanf (" % d " , & a [ i ] ) ;
}
// swap elements
for( i = 0 ; i < n ; i+ = 2 )
{
t = a [ i ] ;
a [ i ] = a [ i + 1 ] ;
a [ i + 1 ] = t ;
}
printf ( " after swapping the elements array will be like : ");
for ( i = 0 ; i < n ; i ++ )
{
printf ( " % d " ,a [ i ] ) ;
}
return;
}