1-Write a simple program to reverse an array.
#include<stdio.h>
#include<conio.h>
int main()
{
int c, d, n, a[100], b[100];
printf ("\n\nEnter number of elements in array :");
scanf ("%d", &n);
printf ("\n\nEnter %d elements\n", n);
for(c = 0; c < n; c++)
scanf ("%d", &a[c]);
for(c = n-1, d = 0; c >= 0; c--, d++)
b[d] = a[c];
for(c = 0; c < n; c++)
a[c] = b[c];
printf("\n\n Resultant array is: ");
for(c = 0; c < n; c++)
printf("%d", a[c]);
return 0;
}
Output:-
Enter number of elements in array :- 5
Enter 5 elements
2
3
4
5
6
Resultant array is: 6 5 4 3 2
Some basic programs of Operators in c language
#include<stdio.h>
#include<conio.h>
int main()
{
int array[100], position, c, n, value;
printf("\n\nEnter number of elements in array:");
scanf("%d", &n);
printf("\n\nEnter %d elements\n", n);
for(c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("\n\nEnter the location where you want to insert new element: ");
scanf("%d", &position);
printf("\n\nEnter the value to insert: ");
scanf("%d", &value);
for(c = n-1; c >= position-1; c--)
array[c+1] = array[c];
array[position - 1] = value; // insertin given value
printf("\n\nResultant array is: ");
for(c = 0; c <= n; c++)
printf("%d ", array[c]);
return 0;
}
Output:-
Enter number of elements in array:- 4
Enter 4 elements
3
4
5
6
Enter the location where you want to insert new element :- 3
Enter the value to insert: 55
Resultant array is: 3 4 55 5 6
#include<stdio.h>
int main ()
{
int array [100], position, c, n;
printf ("\n\nEnter number of elements in array:");
scanf ("%d", &n);
printf ("\n\nEnter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf ("\n\nEnter the location where you want to delete element from: ");
scanf ("%d", &position);
if (position >= n+1)
printf ("\n\nDeletion not possible\n\n");
else
for (c = position-1; c < n-1; c++)
array[c] = array[c+1];
printf("\n\nResultant array is: ");
for (c = 0; c < n-1; c++)
printf("%d ", array[c]);
return 0;
}
Output :-
Enter number of elements in array :-6
Enter 6 elements
2
3
4
5
6
7
Deletion not possible :- 3
Resultant array is: 2 3 5 6 7
#include<stdio.h>
int main()
{
int array[10], element, c, n, pos;
int found = 0;
printf("\n\nEnter number of elements in array:");
scanf("%d", &n);
printf("\n\nEnter %d elements\n", n);
for(c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("\n\nThe input array is: ");
for(c = 0; c < n; c++)
printf("%d", array[c]);
printf("\n\nEnter the element to be deleted: ");
scanf("%d", &element);
for(c = 0; c < n; c++)
{
if(array[c] == element)
{
found = 1;
pos = c;
break; // terminate the loop
}
}
if(found == 1) // the element to be deleted exists in the array
{
for(c = pos; c < n-1; c++)
array[c] = array[c+1];
}
else
printf("\n\nElement %d is not found in the array\n\n", element);
printf("\n\nResultant array is: ");
for(c = 0; c < n-1; c++)
printf("%d ",array[c]);
return 0;
}
Output:- Enter number of elements in array:5
Enter 5 elements
4
5
6
7
8
The input array is: 4 5 6 7 8
Enter the element to be deleted: 6
Resultant array is: 4 5 7 8
Some special and important program of c language and c++
Array program |
#include<stdio.h>
#include<conio.h>
int main()
{
int c, d, n, a[100], b[100];
printf ("\n\nEnter number of elements in array :");
scanf ("%d", &n);
printf ("\n\nEnter %d elements\n", n);
for(c = 0; c < n; c++)
scanf ("%d", &a[c]);
for(c = n-1, d = 0; c >= 0; c--, d++)
b[d] = a[c];
for(c = 0; c < n; c++)
a[c] = b[c];
printf("\n\n Resultant array is: ");
for(c = 0; c < n; c++)
printf("%d", a[c]);
return 0;
}
Output:-
Enter number of elements in array :- 5
Enter 5 elements
2
3
4
5
6
Resultant array is: 6 5 4 3 2
Some basic programs of Operators in c language
2- a simple program to insert an element in an array.
#include<stdio.h>
#include<conio.h>
int main()
{
int array[100], position, c, n, value;
printf("\n\nEnter number of elements in array:");
scanf("%d", &n);
printf("\n\nEnter %d elements\n", n);
for(c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("\n\nEnter the location where you want to insert new element: ");
scanf("%d", &position);
printf("\n\nEnter the value to insert: ");
scanf("%d", &value);
for(c = n-1; c >= position-1; c--)
array[c+1] = array[c];
array[position - 1] = value; // insertin given value
printf("\n\nResultant array is: ");
for(c = 0; c <= n; c++)
printf("%d ", array[c]);
return 0;
}
Output:-
Enter number of elements in array:- 4
Enter 4 elements
3
4
5
6
Enter the location where you want to insert new element :- 3
Enter the value to insert: 55
Resultant array is: 3 4 55 5 6
3- Write a simple program to delete an element from array, where the position of element to be deleted is given by user.
#include<stdio.h>
int main ()
{
int array [100], position, c, n;
printf ("\n\nEnter number of elements in array:");
scanf ("%d", &n);
printf ("\n\nEnter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf ("\n\nEnter the location where you want to delete element from: ");
scanf ("%d", &position);
if (position >= n+1)
printf ("\n\nDeletion not possible\n\n");
else
for (c = position-1; c < n-1; c++)
array[c] = array[c+1];
printf("\n\nResultant array is: ");
for (c = 0; c < n-1; c++)
printf("%d ", array[c]);
return 0;
}
Output :-
Enter number of elements in array :-6
Enter 6 elements
2
3
4
5
6
7
Deletion not possible :- 3
Resultant array is: 2 3 5 6 7
4- a simple program to delete an element from array, where the element to be deleted is given by user.
#include<stdio.h>
int main()
{
int array[10], element, c, n, pos;
int found = 0;
printf("\n\nEnter number of elements in array:");
scanf("%d", &n);
printf("\n\nEnter %d elements\n", n);
for(c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("\n\nThe input array is: ");
for(c = 0; c < n; c++)
printf("%d", array[c]);
printf("\n\nEnter the element to be deleted: ");
scanf("%d", &element);
for(c = 0; c < n; c++)
{
if(array[c] == element)
{
found = 1;
pos = c;
break; // terminate the loop
}
}
if(found == 1) // the element to be deleted exists in the array
{
for(c = pos; c < n-1; c++)
array[c] = array[c+1];
}
else
printf("\n\nElement %d is not found in the array\n\n", element);
printf("\n\nResultant array is: ");
for(c = 0; c < n-1; c++)
printf("%d ",array[c]);
return 0;
}
Output:- Enter number of elements in array:5
Enter 5 elements
4
5
6
7
8
The input array is: 4 5 6 7 8
Enter the element to be deleted: 6
Resultant array is: 4 5 7 8
Some special and important program of c language and c++
0 Comments