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
C program to swap adjacent elements of an one dimensional array.
#include <stdio.h>
#define MAX 100
int main()
{
int arr[MAX],n,i;
int temp;
printf("Enter total number of elements: ");
scanf("%d",&n);
//value of n must be even
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",&arr[i]);
}
//swap adjacent elements
for(i=0;i < n;i+=2)
{
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1]= temp;
}
printf("\nArray elements after swapping adjacent elements:\n");
for(i=0;i < n;i++)
{
printf("%d\n",arr[i]);
}
return;
}
Likewise the c program can be written for the above example you have mentioned by including the condition of the inputs to be given.
You can increase the number of inputs you wanted by changing the MAX value that is defined in the program
..
C program to swap adjacent elements of an one dimensional array.
#include <stdio.h>
#define MAX 100
int main()
{
int arr[MAX],n,i;
int temp;
printf("Enter total number of elements: ");
scanf("%d",&n);
//value of n must be even
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",&arr[i]);
}
//swap adjacent elements
for(i=0;i < n;i+=2)
{
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1]= temp;
}
printf("\nArray elements after swapping adjacent elements:\n");
for(i=0;i < n;i++)
{
printf("%d\n",arr[i]);
}
return;
}
Likewise the c program can be written for the above example you have mentioned by including the condition of the inputs to be given.
You can increase the number of inputs you wanted by changing the MAX value that is defined in the program