#include<stdio.h> #include<conio.h> void main() { int i,a[50],sum,n; printf("n Enter no of elements :"); scanf("%d",&n); /* Reading values into Array */ printf("n Enter the values :"); for(i=0;i < n;i++) scanf("%d",&a[i]); /* computation of total */ sum=0; for(i=0;i < n;i++) sum=sum+a[i]; /* printing of all elements of array */ for(i=0;i < n;i++) printf("n a[%d]=%d",i,a[i]); /* printing of total */ printf("n sum=%d",sum); getch(); }
C Program to Print Array Elements
C Programs#include<stdio.h> #include<conio.h> void main() { int i,a[50],sum,n; printf("n Enter no of elements :"); scanf("%d",&n); /* Reading values into Array */ printf("n Enter the values :"); for(i=0;i < n;i++) scanf("%d",&a[i]); /* computation of total */ sum=0; for(i=0;i < n;i++) sum=sum+a[i]; /* printing of all elements of array */ for(i=0;i < n;i++) printf("n a[%d]=%d",i,a[i]); /* printing of total */ printf("n sum=%d",sum); getch(); }
C Program to Delete an element from the specified location from Array
// n - no.of elements in array // i - for traversing the array // j - location of the element to be deleted // a - Array #include<stdio.h> #include<conio.h> void main() { int a[30],n,i,j; printf("n Enter no of elements :"); scanf("%d",&n); /* read n elements in an array */ printf("n Enter %d elements :",n); for(i=0;i 〈 n;i++) scanf("%d",&a[i]); /* read the location of the element to be deleted */ printf("n location of the element to be deleted :"); scanf("%d",&j); /* loop for the deletion */ while(j 〈 n) { a[j-1]=a[j]; j++; } n--; /* no of elements reduced by 1 */ /* loop for printing */ for(i=0;i 〈 n;i++) printf("n %d",a[i]); ge
C Program to Insert an element in an Array
#include<stdio.h> int main() { int arr[30],element,num,i,location; printf("n Enter no of elements :"); scanf("%d",&num); for(i=0 ; i < num ; i++) scanf("%d",&arr[i]); printf("n Enter the element to be inserted :"); scanf("%d",&element); printf("n Enter the location"); scanf("%d",&location); /* create space at the specified location */ for(i = num ;i >= location ; i--) arr[i] = arr[i-1]; num++; arr[location-1] = element; /* Print out the Result of Insertion */ for(i = 0 ;i < num ;i++) printf("n %d",arr[i]); return(0); }
Output of the Program :
Enter no of elements : 5 1 2 3 4 5 Enter the element to be inserted : 6 Enter the location : 2 1 6 2 3 4 5
C Program to Copy all elements of an array into Another array
//Title : Copy Array Elements from one array to another //a - Source Array ( elements will be copied from this array) //b - Destination array ( elements copied into this array) //--------------------------------------------------------------------- #include<stdio.h> #include<conio.h> void main() { int a[30],b[30],i,n; // Element of an array 'a' will be copied into array 'b' printf("n Enter no of elements :"); scanf("%d",&n); /* Accepting values into Array a*/ printf("n Enter the values :"); for(i = 0 ; i < n ; i++) scanf("%d",&a[i]); /* Copying data from array 'a' to array 'b */ for(i = 0 ;i < n ; i++) b[i]=a[i]; /* printing of all elements of array */ printf("the copied array is :"); for(i=0;i < n;i++) printf("nb[%d]=%d",i,b[i]); getch(); }
C Program to Search an element in Array
#include<stdio.h> #include<conio.h> void main() { int a[30],x,n,i; /* a - for storing of data x - element to be searched n - no of elements in the array i - scanning of the array */ printf("nEnter no of elements :"); scanf("%d",&n); /* Reading values into Array */ printf("nEnter the values :"); for(i=0;i < n;i++) scanf("%d",&a[i]); /* read the element to be searched */ printf("nEnter the elements to be searched"); scanf("%d",&x); /* search the element */ i=0; /* search starts from the zeroth location */ while(i < n && x!=a[i]) i++; /* search until the element is not found i.e. x!=a[i] search until the element could still be found i.e. i 〈 n */ if(i < n) /* Element is found */ printf("found at the location =%d",i+1); else printf("n not found"); getch(); }
Subscribe to:
Posts (Atom)