Insert Element in An Array : C Program

Computer Science » C Program » Insert Element in An Array

Program :

#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);
}

Output :

Enter no of elements : 5
10 20 30 40 50
Enter the element to be inserted : 60
Enter the location : 3
 10
 20
 60
 30
 40
 50
Subject Name : Computer Science
Exam Name : IIT GATE, UPSC ESE, RRB, SSC, DMRC, NMRC, BSNL, DRDO, ISRO, BARC, NIELIT
Posts Name : Assistant Engineer, Management Trainee, Junior Engineer, Technical Assistant
Computer Science Books

GATE 2023 Total InfoENGG DIPLOMAUGC NET Total Info
IES 2023 Total InfoPSUs 2022 Total InfoCSIR UGC NET Total Info
JAM 2023 Total InfoM TECH 2023 Total InfoRAILWAY 2022 Total Info

Related Posts

  • Searching Element in an Array : C ProgramComputer Science » C Program » Searching Element in an Array Program : [php]#include<stdio.h> int main() { int a[30], element, num, i; printf("nEnter no of elements : "); scanf("%d", &num); printf("nEnter the values : "); for (i = 0; i < num; i++) { scanf("%d", &a[i]); } //Read the element to be searched printf("nEnter the element to be searched : "); scanf("%d", &element); //Search starts from the zeroth location i = 0; while (i < num && element != a[i]) { i++; } //If i < num then Match found if (i < num) { printf("nNumber found at the location : %d", i + 1); } else { printf("nNumber not found."); } return (0); }[/php] Output 1: [php]Enter no of elements : 5 Enter the values : 10 20 30 40 50 Enter the element to be searched : 30 Number found at the location : 3[/php] Output 1: [php]Enter no of elements : 5 Enter the values : 10 20 30 40 50 Enter the element to be searched : 60 Number not found.[/php]
    Tags: program, element, array, output, computer, science, programs
  • 1-10 Numbers in Pyramid : C ProgramComputer Science » C Program » 1-10 Numbers in Pyramid Program : [php]#include<stdio.h> int main() { int i, j; int count = 1; for (i = 0; i <= 4; i++) { printf("n"); for (j = 0; j < i; j++) { printf("%dt", count); count++; } } return(0); }[/php] Output : [php]1 2 3 4 5 6 7 8 9 10 [/php]
    Tags: program, computer, science, output, programs
  • Check Whether a Number is Prime or Not : C ProgramComputer Science » C Program » Check Whether a Number is Prime or Not  Program : [php]#include<stdio.h> int main() { int num, i, count = 0; printf("nEnter a number :"); scanf("%d", &num); for (i = 2; i <= num / 2; i++) { if (num % i == 0) { count++; break; } } if (count == 0) printf("n%d is a prime number.", num); else printf("n%d is not a prime number.", num); return 0; }[/php] Output 1: [php]Enter a number : 5 5 is a prime number.[/php] Output 2: [php]Enter a number : 4 4 is not a prime number.[/php]
    Tags: program, output, computer, science, programs
  • Check Whether a Number is Perfect or Not : C ProgramComputer Science » C Program » Check Whether a Number is Perfect or Not Program : [php]#include&lt;stdio.h&gt; int main() { int num, i = 1, sum = 0; printf(&quot;nEnter a number : &quot;); scanf(&quot;%d&quot;, &amp;num); while (i &lt; num) { if (num % i == 0) { sum = sum + i; } i++; } if (sum == num) printf(&quot;n%d is a perfect number.&quot;, i); else printf(&quot;n%d is not a perfect number.&quot;, i); return 0; }[/php] Output 1: [php]Enter a number : 6 6 is a perfect number.[/php] Output 2: [php]Enter a number : 5 is not a perfect number.[/php]
    Tags: program, output, computer, science, programs
  • Check Whether a Number is Armstrong or Not : C ProgramComputer Science » C Program » Check Whether a Number is Armstrong or Not  Program : [php]#include<stdio.h> int main() { int num, temp, sum = 0, rem; printf("nEnter a number : "); scanf("%d", &num); temp = num; while (num != 0) { rem = num % 10; sum = sum + (rem * rem * rem); num = num / 10; } if (temp == sum) printf("n%d is a amstrong number.", temp); else printf("n%d is not a amstrong number", temp); return (0); }[/php] Output 1: [php]Enter a number : 153 153 is a amstrong number.[/php] Output 2: [php]Enter a number : 150 150 is not a amstrong number[/php]
    Tags: program, output, computer, science, programs

LEAVE A REPLY

Please enter your comment!
Please enter your name here