CODE:
- #include<stdio.h>
- int main()
- {
- int i1,i2;
- int n;
- printf("Enter the size of array: \n");
- scanf("%d", &n);
- int arr[n];
- for (int i = 0; i < n; i++)
- {
- printf("Enter element with %d : ", i);
- scanf("%d", &arr[i]);
- }
- printf("Enter two index you want to swap: \n");
- scanf("%d %d", &i1, &i2);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (i==i1 && j==i2)
- {
- int temp=arr[i];
- arr[i]=arr[j];
- arr[j]=temp;
- }
- }
- }
- printf("After swapping the two elements, the array is like this: \n");
- for (int i = 0; i < n; i++)
- {
- printf("%d ---------> %d\n", i, arr[i]);
- }
- return 0;
- }
OUTPUT:
Enter the size of array:
4
Enter element with 0 : 1
Enter element with 1 : 3
Enter element with 2 : 5
Enter element with 3 : 7
Enter two index you want to swap:
1
3
After swapping the two elements, the array is like this:
0 ---------> 1
1 ---------> 7
2 ---------> 5
3 ---------> 3