---
title: "Insert number at position in array"
subject: "Computer Programming"
chapter: 11
key: "semester_1/computer_programming:fortran-insert-number-in-array"
source: https://www.bctengineer.com/q/semester_1/computer_programming/fortran-insert-number-in-array
site: BCT Engineering
---

# Insert number at position in array

Computer Programming · Chapter 11: Programming Language: FORTRAN

- Asked in: 2067 Asoj, 8 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/fortran-insert-number-in-array
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-11

## Question

Write a program in FORTRAN to take a position and a number and insert this number on this position inside an array containing n elements.

## Short answer

Shift all elements from POS to N one place right using a backward loop (I = N down to POS), then store the number at A(POS) and increment N. Validity checks ensure position is between 1 and N+1 and array size is not exceeded.

## Answer

**Method.**
1. Read n and the n elements into array A (declared larger than n, e.g. 100).
2. Read the position POS (1 to n + 1) and the number NUM.
3. Starting from the last element, move each element from POS to n one place to the right (`A(I+1) = A(I)` for I = n down to POS). The loop runs backwards (step −1) so no value is overwritten.
4. Store NUM in `A(POS)` and increase n by 1.
5. Print the new array.

```fortran
      PROGRAM INSERT
      INTEGER A(100), N, POS, NUM, I
      PRINT *, 'ENTER NUMBER OF ELEMENTS'
      READ *, N
      PRINT *, 'ENTER THE ELEMENTS'
      READ *, (A(I), I = 1, N)
      PRINT *, 'ENTER POSITION AND NUMBER TO INSERT'
      READ *, POS, NUM
      IF (POS .LT. 1 .OR. POS .GT. N+1 .OR. N .GE. 100) THEN
         PRINT *, 'INSERTION NOT POSSIBLE'
         STOP
      END IF
      DO 10 I = N, POS, -1
         A(I+1) = A(I)
   10 CONTINUE
      A(POS) = NUM
      N = N + 1
      PRINT *, 'NEW ARRAY:'
      PRINT *, (A(I), I = 1, N)
      STOP
      END
```

**Sample:** array 10 20 30 40 50, position 3, number 99 gives `10 20 99 30 40 50`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which elements are shifted to make room for the new number?
   A. Elements from POS to N one place right
   B. Elements 1 to POS left
   C. All elements to the end
   D. Only A(N)
   Answer: A. Elements from POS to N each move one place to the right. (Key point: Shift direction)

2. Why does the loop run backwards, I = N, POS, -1?
   A. So that no value is overwritten before it has been moved
   B. To sort the array
   C. Because step must be negative
   D. To reverse the array
   Answer: A. Moving from the end, A(I+1) = A(I) copies into an already-moved or empty slot. (Key point: Loop direction)

3. What range of POS is allowed?
   A. 1 to N + 1
   B. 0 to N
   C. 1 to N - 1
   D. Any integer
   Answer: A. The number can go before the first element up to just after the last one. (Key point: Valid position)

4. Array 10 20 30 40 50, position 3, number 99 gives?
   A. 10 20 99 30 40 50
   B. 10 99 20 30 40 50
   C. 10 20 30 99 40 50
   D. 99 10 20 30 40 50
   Answer: A. 99 is placed at position 3, with 30, 40, 50 shifted right. (Key point: Sample)

## Related in Computer Programming

- [Insert value in array at position](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-insert-algorithm) — Computer Programming, chapter 7
- [Sort array ascending in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-sort-ascending) — Computer Programming, chapter 11, asked 2×
- [Structure of a FORTRAN program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-structure) — Computer Programming, chapter 11, asked 5×
- [Accessing array elements using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-access-array-elements) — Computer Programming, chapter 9
- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3
- [Insert a string into another string](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Ainsert-string-into-string) — Computer Programming, chapter 7

## Related in other subjects

- [Prime number program: procedural and OOP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprime-procedural-oop) — Object Oriented Programming, chapter 1
- [Insert and delete kth node in doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-insert-delete-kth) — Data Structure and Algorithm, chapter 4, asked 2×
- [Function template: sum and average of array](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-sum-average) — Object Oriented Programming, chapter 9
