Home / Programs / C Program to Insert an element in an Array
🚀 Programming Example

C Program to Insert an element in an Array

👁 1,230 Views
💻 Practical Program
📘 Step Learning
C Program to Insert an element in an Array

💻 Program Code

#include<stdio.h>
 
int main() {
   int arr[30], element, num, i, location;
 
   printf("\nEnter no of elements :");
   scanf("%d", &num);
 
   for (i = 0; i < num; i++) {
      scanf("%d", &arr[i]);
   }
 
   printf("\nEnter the element to be inserted :");
   scanf("%d", &element);
 
   printf("\nEnter the location");
   scanf("%d", &location);
 
   //Create space at the specified location
   for (i = num; i >= location; i--) {
      arr[i] = arr[i - 1];
   }
 
   num++;
   arr[location - 1] = element;
 
   //Print out the result of insertion
   for (i = 0; i < num; i++)
      printf("\n %d", arr[i]);
 
   return (0);
}
                        

🖥 Program Output


Enter no of elements :5
1
2
3
4
5

Enter the element to be inserted :9

Enter the location 2

 1
 9
 2
 3
 4
 5Press any key to continue . . .
                            

📘 Explanation

none
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.