Program to find sum of array elements.
#include<stdio.h>
int main(){
int ary[10], n, i, sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
for(i=0;i<n;i++)
sum = sum + ary[i];
printf("%d",sum);
return 0;
}
5
6 2 7 8 3
26
Program to insert an element at given position in array.
#include<stdio.h>
int main(){
int ary[50],n,i,pos,newEle;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
scanf("%d",&pos);
scanf("%d",&newEle);
if(pos<=n){
for(i=n;i>pos;i--)
ary[i] = ary[i-1];
ary[pos] = newEle;
n++;
for(i=0;i<n;i++)
printf("%d ",ary[i]);
}
else
printf("Insertion is not possible.");
return 0;
}
5
5 15 20 25 30
1
10
5 10 15 20 25 30
Program to delete an element from an array at specified position.
#include<stdio.h>
int main(){
int ary[50],n,i,pos;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
scanf("%d",&pos);
if(pos<n){
for(i=pos;i<n;i++)
ary[i] = ary[i+1];
n--;
for(i=0;i<n;i++)
printf("%d ",ary[i]);
}
else
printf("Deletion is not possible.");
return 0;
}
5
5 15 20 25 30
1
5 20 25 30
Program to reverse of array elements.
#include<stdio.h>
int main(){
int ary[10], n, i, j, temp;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
for(i=0,j=n-1;i<j;i++,j--){
temp = ary[i];
ary[i] = ary[j];
ary[j] =temp;
}
for(i=0;i<n;i++)
printf("%d ",ary[i]);
return 0;
}
5
6 2 7 8 3
3 8 7 2 6
Program to remove duplicates in array.
#include<stdio.h>
int main(){
int ary[10], n, i, j, k;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(ary[i]==ary[j]){
for(k=j;k<n;k++)
ary[k] = ary[k+1];
j--;
n--;
}
}
}
for(i=0;i<n;i++)
printf("%d ",ary[i]);
return 0;
}
6
1 2 4 3 2 3
1 2 4 3
Program to move first element to last in array.
#include<stdio.h>
int main(){
int ary[20], n, i, temp;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
temp = ary[0];
for(i=0;i<n-1;i++)
ary[i] = ary[i+1];
ary[i] = temp;
for(i=0;i<n;i++)
printf("%d ",ary[i]);
return 0;
}
6
1 2 4 3 2 3
2 4 3 2 3 1
Program to count the frequency of each element of an array.
#include<stdio.h>
int main(){
int ary[20], n, i, j, k, freq;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
for(i=0;i<n-1;i++){
freq=1;
for(j=i+1;j<n;j++){
if(ary[i]==ary[j]){
freq++;
for(k=j;k<n-1;k++)
ary[k] = ary[k+1];
j--;
n--;
}
}
printf("%d - %d\n",ary[i],freq);
}
return 0;
}
6
1 2 4 3 2 3
1 - 1
2 - 2
4 - 1
3 - 2
Program to sort the elements in array.
#include<stdio.h>
int main(){
int ary[20], n, i, j, temp;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(ary[i]>ary[j]){
temp = ary[i];
ary[i] = ary[j];
ary[j] = temp;
}
}
}
for(i=0;i<n;i++)
printf("%d ",ary[i]);
return 0;
}
6
1 2 4 3 2 3
1 2 2 3 3 4
Program to find second smallest and second largest numbers from array.
#include<stdio.h>
int main(){
int ary[20], n, i, j, temp;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(ary[i]>ary[j]){
temp = ary[i];
ary[i] = ary[j];
ary[j] = temp;
}
}
}
printf("%d %d",ary[1],ary[n-2]);
return 0;
}
6
6 2 7 3 5 4
3 6
Program for left rotate of array.
#include<stdio.h>
int main(){
int ary[20], n, i, noOfRotations, j, temp;
scanf("%d",& n);
for(i=0;i<n;i++)
scanf("%d",& ary[i]);
scanf("%d",& noOfRotations);
for(j=1;j<=noOfRotations;j++){
temp = ary[0];
for(i=0;i<n-1;i++)
ary[i] = ary [i+1];
ary[i] = temp;
}
for(i=0;i<n;i++)
printf("%d ",ary[i]);
return 0;
}
6
6 2 1 3 5 4
2
1 3 5 4 6 2
Program for right rotate of array.
#include<stdio.h>
int main(){
int ary[20], n, i, noOfRotations, j, temp;
scanf("%d",& n);
for(i=0;i<n;i++)
scanf("%d",& ary[i]);
scanf("%d",& noOfRotations);
for(j=1;j<=noOfRotations;j++){
temp = ary[n-1];
for(i=n-1;i>0;i--)
ary[i] = ary [i-1];
ary[i] = temp;
}
for(i=0;i<n;i++)
printf("%d ",ary[i]);
return 0;
}
6
6 2 1 3 5 4
2
5 4 6 2 1 3
Program to find duplicate element in k-distance. If found print "YES" otherwise "NO".
#include<stdio.h>
int main(){
int ary[50],i,j,k,n,flag=0;
scanf("%d %d",&n,&k);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
for(i=0;i<=n-k;i++){
for(j=i+1;j<=(i+k);j++){
if(ary[i]==ary[j]){
flag=1;
break;
}
}
}
if(flag==1)
printf("YES");
else
printf("NO");
return 0;
}
6 3
1 3 4 6 3 2
YES
Program that calculate the fractions of its elements that are positive, negative and zeros.
#include <stdio.h>
int main(){
int ary[50],n,i,cp=0,cn=0,cz=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
for(i=0;i<n;i++){
if(ary[i]>0)
cp++;
else if(ary[i]<0)
cn++;
else
cz++;
}
printf("%f",(float)cp/n);
printf("\n%f",(float)cn/n);
printf("\n%f",(float)cz/n);
return 0;
}
6
-4 3 -9 0 4 1
0.500000
0.333333
0.166667
Arrays
Program to delete an element from an array at specified position.
Program to find second smallest and second largest numbers from array.
Program to find duplicate element in k-distance. If found print "YES" otherwise "NO".
Program that calculate the fractions of its elements that are positive, negative and zeros.