1. Write a program in C to print all unique elements in an array.
SOURCE CODE:
OUTPUT:
2. Write a program in C to separate odd and even integers in separate arrays.
SOURCE CODE:
3. Write a program in C to sort elements of array in ascending order.
SOURCE CODE:
SOURCE CODE:
#include<stdio.h>
int main(){
int a[20],n,i,j,k;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]==a[j]){
for(k=j;k<n-1;k++)
a[k]=a[k+1];
n--;
j--;
}
}
}
printf("\nUnique elements in array are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
}
int main(){
int a[20],n,i,j,k;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]==a[j]){
for(k=j;k<n-1;k++)
a[k]=a[k+1];
n--;
j--;
}
}
}
printf("\nUnique elements in array are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
}
OUTPUT:
Enter array size:5
Enter array elements:
1 2 3 2 1
Unique elements in array are:
1 2 3
Enter array elements:
1 2 3 2 1
Unique elements in array are:
1 2 3
2. Write a program in C to separate odd and even integers in separate arrays.
SOURCE CODE:
#include<stdio.h>
int main(){
int arr[30],evenArr[30],oddArr[30],i,j=0,k=0,ea=0,oa=0,n;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++){
if(arr[i]%2==0)
evenArr[j++]=arr[i];
else
oddArr[k++]=arr[i];
}
printf("Even elements in the array:\n");
for(i=0;i<j;i++)
printf("%d\t",evenArr[i]);
printf("\nOdd elements in the array:\n");
for(i=0;i<k;i++)
printf("%d\t",oddArr[i]);
return 0;
}
OUTPUT:int main(){
int arr[30],evenArr[30],oddArr[30],i,j=0,k=0,ea=0,oa=0,n;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++){
if(arr[i]%2==0)
evenArr[j++]=arr[i];
else
oddArr[k++]=arr[i];
}
printf("Even elements in the array:\n");
for(i=0;i<j;i++)
printf("%d\t",evenArr[i]);
printf("\nOdd elements in the array:\n");
for(i=0;i<k;i++)
printf("%d\t",oddArr[i]);
return 0;
}
Enter array size:5
Enter 5 elements:
10 25 30 35 55
Even elements in the array:
10 30
Odd elements in the array:
25 35 55
Enter 5 elements:
10 25 30 35 55
Even elements in the array:
10 30
Odd elements in the array:
25 35 55
3. Write a program in C to sort elements of array in ascending order.
SOURCE CODE:
#include<stdio.h>
int main(){
int arr[20],n,i,j,temp;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Before sorting array elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\nAfter sorting array elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
return 0;
}
OUTPUT:int main(){
int arr[20],n,i,j,temp;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Before sorting array elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\nAfter sorting array elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
return 0;
}
Enter array size:5
Enter 5 elements:
3 4 2 5 1
Before sorting array elements are:
3 4 2 5 1
After sorting array elements are:
1 2 3 4 5
Enter 5 elements:
3 4 2 5 1
Before sorting array elements are:
3 4 2 5 1
After sorting array elements are:
1 2 3 4 5