#include<stdio.h>
int main()
{
int i, a[100], n, item, position;
printf("How many array elements you want to enter\n");
scanf("%d", &n);
printf("Enter an array elements\n");
for(i = 0; i<n; i++)
scanf("%d", &a[i]);
printf("which position you want to insert an element\n");
scanf("%d", &position); //Here position is array Index Number
printf("Enter new array Element to insert\n");
scanf("%d",&item);
if(position > n)
{
printf("New array Element cannot be Inserted at this Position");
}
else if(position = = n)
{
a[position] = item;
n++;
}
else
{
for(i = n; i > position; i--)
{
a[i] = a[i-1];
}
a[position] = item;
n++ ;
}
printf("The Resultant array after inserting Element is\n");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
INPUT 1:
How many array elements you want to enter
5
Enter an array elements
11
22
33
44
55
which position you want to insert an element
3
Enter new array Element to insert
57
OUTPUT 1:
The Resultant array after inserting Element is
11 22 33 57 44 55
INPUT 2:
How many array elements you want to enter
5
Enter an array elements
11
22
33
44
55
which position you want to insert an element
7
Enter new array Element to insert
57
OUTPUT 2:
New array Element cannot be Inserted at this Position
int main()
{
int i, a[100], n, item, position;
printf("How many array elements you want to enter\n");
scanf("%d", &n);
printf("Enter an array elements\n");
for(i = 0; i<n; i++)
scanf("%d", &a[i]);
printf("which position you want to insert an element\n");
scanf("%d", &position); //Here position is array Index Number
printf("Enter new array Element to insert\n");
scanf("%d",&item);
if(position > n)
{
printf("New array Element cannot be Inserted at this Position");
}
else if(position = = n)
{
a[position] = item;
n++;
}
else
{
for(i = n; i > position; i--)
{
a[i] = a[i-1];
}
a[position] = item;
n++ ;
}
printf("The Resultant array after inserting Element is\n");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
INPUT 1:
How many array elements you want to enter
5
Enter an array elements
11
22
33
44
55
which position you want to insert an element
3
Enter new array Element to insert
57
OUTPUT 1:
The Resultant array after inserting Element is
11 22 33 57 44 55
INPUT 2:
How many array elements you want to enter
5
Enter an array elements
11
22
33
44
55
which position you want to insert an element
7
Enter new array Element to insert
57
OUTPUT 2:
New array Element cannot be Inserted at this Position
No comments:
Post a Comment