fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int n, i, position, newValue;
  5. printf("Enter the size of the array: ");
  6. scanf("%d", &n);
  7.  
  8. int arr[n];
  9. printf("Enter %d elements of the array:\n", n);
  10. for (i = 0; i < n; i++) {
  11. scanf("%d", &arr[i]);
  12. }
  13.  
  14. printf("Enter the position to replace (1 to %d): ", n);
  15. scanf("%d", &position);
  16.  
  17. if (position < 1 || position > n) {
  18. printf("Invalid position!\n");
  19. return 1;
  20. }
  21.  
  22. printf("Enter the new value: ");
  23. scanf("%d", &newValue);
  24.  
  25. // Replace the value
  26. arr[position - 1] = newValue;
  27.  
  28. printf("Updated array:\n");
  29. for (i = 0; i < n; i++) {
  30. printf("%d ", arr[i]);
  31. }
  32. printf("\n");
  33.  
  34. }
  35.  
Success #stdin #stdout 0s 5284KB
stdin
5
1 2 3 4 5
4
100
stdout
Enter the size of the array: Enter 5 elements of the array:
Enter the position to replace (1 to 5): Enter the new value: Updated array:
1 2 3 100 5