You are on page 1of 12

1. Write a program to print the elements of array (Traversing).

#include<stdio.h> #include<conio.h> void main() { int a[100],i=0,n; printf("\n How many numbers you want-"); scanf("%d",&n); printf("\n enter the elements"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\n the elements of the array are\n"); for(i=0;i<n;i++) { printf("%d\n",a[i]); } getch(); clrscr(); }

OUTPUT

2.Write a program to count the even or odd number in array.


#include<stdio.h> #include<conio.h> void main() { int a[8],count_odd=0,count_even=0,i; printf("\n Enter the elements"); for(i=0;i<8;i++) { scanf("%d",&a[i]); } for(i=0;i<8;i++) { if(a[i]%2==0) count_even++; else count_odd++;

} printf(" \n %d The even number in array ",count_even); printf("\n %d The odd number in array ",count_odd); getch(); clrscr(); }

3. Write a program to count the prime number in array.


#include<stdio.h> #include<conio.h> void main() { int a[100],i,n,j,counter,sum=0; clrscr(); printf("How many number do you want_\n"); scanf("%d",&n); printf("enter the elements of array_\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Elements in array are_\n"); for(i=0;i<n;i++) printf("\t%d",a[i]); printf("\n the prime number in array are_\n");

for(i=0;i<n;i++) { counter=0; for(j=2;j<=a[i]/2;j++) { if(a[i]%j==0) { counter=1; break; } } if(counter==0) printf("\t%d",a[i]); } getch(); }

4. Write a program to print the sum of elements of array.

#include<stdio.h> #include<conio.h> void main() { int a[100],sum=0,n,i;

printf("\n Enter the number of elements you want"); scanf("%d",&n); printf("\n Enter the elements"); for(i=0;i<n;i++) { scanf("%d",&a[i]); sum=sum+a[i]; } printf("\n The sum of elements of array is %d",sum); getch(); clrscr(); }

5.Write a program to determine the address of arrays element.


#include<stdio.h> #include<conio.h> void main() { int a[5],i,p; printf("enter ");

for(i=0;i<5;i++) { scanf("%d",&a[i]); } for(i=0;i<5;i++) { printf("a[%d] is at %p\n",i,&a[i]); } getch(); clrscr(); }

6.Write a program for linear search.


#include<stdio.h> #include<conio.h> void main() { int a[100],i,n,m,c=0; printf("\n Enter the size of an array"); scanf("%d",&n); printf("\n Enter the elements");

for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\n Enter the element you want to search"); scanf("%d",&m); for(i=0;i<n;i++) { if(a[i]==m) { c=1; break; } } if(c==0) printf("\n The number is not found"); else printf("\n The number is found"); getch(); clrscr(); }

7.Write a program for binary search.

#include<stdio.h> #include<conio.h> void main() { int a[20],item,big,end,lb,ub,mid,loc,j,k,temp; clrscr(); printf("enter the lower and upper bond of array\n"); scanf("%d%d",&lb,&ub); printf("enter the elements of array\n"); for(j=lb;j<ub;j++) { scanf("%d",&a[j]); } for(j=lb;j<ub-1;j++) { for(k=j+1;k<ub;k++) { if(a[j]>a[k]) { temp=a[j]; a[j]=a[k];

a[k]=temp; } } } printf("\n Array after sorting\n"); for(j=lb;j<ub;j++) { printf("%d\t",a[j]); } printf("\n Enter the item for which you want to search\n"); scanf("%d",&item); big=lb; end=ub; mid=int((big+end)/2); while(big<=end&&a[mid]!=item) { if(item<a[mid]) { end=mid-1; } else { big=mid+1;

} mid=int((big+end)/2); } if(a[mid]==item) { loc=mid; printf("The location of item is=%d",loc); } else { printf("The location of item =0"); } getch(); }

8.Write a program to count the occurrence of given number in array.

#include<stdio.h> #include<conio.h> void main() { int a[10],n,m,i,p,count=0; printf("\n enter the size of array");

scanf("%d",&n); printf("\n enter the elements of array"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n enter the element to be count"); scanf("%d",&p); for(i=0;i<n;i++) { if(p==a[i]) count++; } printf("\n %d is exists %d times in the array",p,count); getch(); clrscr(); }

9. Write the program to count the distinct number in array.

#include<stdio.h> #include<conio.h> int main() { int a[100],distinct=1,i,n; clrscr(); printf("enter the order of array_\n");

scanf("%d",&n); printf("enter the elements of array_\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n the elements of array are_\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); for(i=0;i<n;i++) { if(a[i]==a[i+1]) continue; else distinct++; } printf("\n \n The number of distinct elements is\n=%d",distinct); getch(); return 0; }

You might also like