Put Even & Odd Elements of an Array in 2 Separate Arrays
#include
#include
void main()
{
long int arr[10], oar[10], ear[10];
int i, j = 0, k = 0, n;
clrscr();
printf("Enter the size of array ar \n");
scanf("%d", &n);
printf("Enter the elements of the array \n");
for (i = 0; i < n; i++)
{
scanf("%ld", &arr[i]);
fflush(stdin);
}
for (i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
{
ear[j] = arr[i];
j++;
}
else
{
oar[k] = arr[i];
k++;
}
}
printf("The elements of oar are \n");
for (i = 0; i < j; i++)
{
printf("%ld\n", oar[i]);
}
printf("the elements of ear are \n");
for (i = 0; i < k; i++)
{
printf("%ld\n", ear[i]);
}
getch();
}
Output
Enter the size of array ar
6
Enter the elements of the array
34
56
78
90
12
39
the elements of oar are
39
1
32768
11542516
11210377
The elements of ear are
34
Insert an Element in a Specified Position in a given Array
#include
#include
void main()
{
int array[10];
int i, j, n, m, temp, key, pos;
clrscr();
printf("Enter how many elements \n");
scanf("%d", &n);
printf("Enter the elements \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements are \n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf("Sorted list is \n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
printf("Enter the element to be inserted \n");
scanf("%d", &key);
for (i = 0; i < n; i++)
{
if (key < array[i])
{
pos = i;
break;
}
}
m = n - pos + 1 ;
for (i = 0; i <= m; i++)
{
array[n - i + 2] = array[n - i + 1] ;
}
array[pos] = key;
printf("Final list is \n");
for (i = 0; i < n + 1; i++)
{
printf("%d\n", array[i]);
}
getch();
}
Output
Enter how many elements
5
Enter the elements
76
90
56
78
12
Input array elements are
76
90
56
78
12
Sorted list is
12
56
76
78
90
Enter the element to be inserted
61
Final list is
12
56
61
76
78
90
Cyclically Permute the Elements of an Array
#include
#include
void main ()
{
int i, n, number[30];
clrscr();
printf("Enter the value of the n = ");
scanf("%d", &n);
printf("Enter the numbers\n");
for (i = 0; i < n; ++i)
{
scanf("%d", &number[i]);
}
number[n] = number[0];
for (i = 0; i < n; ++i)
{
number[i] = number[i + 1];
}
printf("Cyclically permuted numbers are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
getch();
}
Output
Enter the value of the n = 4
Enter the numbers
3
40
100
68
Cyclically permuted numbers are given below
40
100
68
3
Sort the Array in an Ascending Order
#include
#include
void main()
{
int i, j, a, n, number[30];
clrscr();
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
getch();
}
Output
Enter the value of N
6
Enter the numbers
3
78
90
456
780
200
The numbers arranged in ascending order are given below
3
78
90
200
456
780
No comments:
Post a Comment