fork download
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int* ptr;
  6. int n,j,i,num,v;
  7.  
  8. printf("Enter number of elements:");
  9. scanf("%d",&n);
  10. printf("Entered number of elements: %d\n", n);
  11. ptr = (int*)malloc(n * sizeof(int));
  12.  
  13. for (i = 0; i < n; ++i) {
  14. scanf("%d", &v);
  15. ptr[i] = v;
  16. }
  17. i=0;
  18. j=0;
  19. while(i<n && j<n){
  20.  
  21. if (ptr[j]%2==0){
  22. num=ptr[i];
  23. ptr[i]=ptr[j];
  24. ptr[j]=num;
  25. i+=2;
  26. j=i;
  27. }
  28. j++;
  29.  
  30. }
  31.  
  32. printf("The elements of the array are: ");
  33. for (i = 0; i < n; ++i) {
  34. printf("%d, ", ptr[i]);
  35. }
  36. }
Success #stdin #stdout 0s 5668KB
stdin
3 1 4 2
stdout
Enter number of elements:Entered number of elements: 3
The elements of the array are: 4, 1, 2,