1. Program to accept elements in an array and display it
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring array variable with max size=10 int a[10]; // Declaring variable i = to iterate loop int i; // Inputting elements printf("Enter 10 elements in an array : "); for(i=0; i<10; i++) { scanf("%d",&a[i]); } // Displaying entered elements printf("Entered elements are : "); for(i=0; i<10; i++) { printf("%d ",a[i]); } // to hold the screen defined in conio.h
getch(); // Linux user - Remove this return 0; }
2. Program to accept elements in an array and display sum of all the elements
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring array variable with size=10 int a[10]; // Declaring variable s=sum to store the sum, i = to iterate loop int i, s=0; // Inputting elements printf("Enter 10 elements in an array : "); for(i=0; i<10; i++) { scanf("%d",&a[i]); } // Displaying entered elements printf("Entered elements are : "); for(i=0; i<10; i++) { printf("%d ",a[i]); } // Performing sum of all elements for(i=0; i<10; i++) { s = s + a[i]; } // Displaying result printf("\nSum of all elements : %d ",s); getch(); // Linux user - Remove this return 0; }
3. Program to insert element in between an array. ( INSERTION )
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring variable "i" to iterate loop int i; // Declaring variable pos=postion from where we want to delete element int pos; // Declaring n=number which we want to insert and "m" to store "n" int n,m; // Declaring and initialising array int a[6]={2,4,6,8,10}; // Printing values in array before insertion printf("Elements before deletion\n"); for(i=0;i<5;i++) { printf("%d ",a[i]); } printf("\n"); // Inputting position printf("Enter the position of element for insertion : "); scanf("%d",&pos); // Inputting value printf("Enter the value which you want to insert : "); scanf("%d",&n); m=n; // Performing insertion for(i=5;i>=pos-1;i--) { a[i+1]=a[i]; } a[pos-1]=m; // Printing elements after insertion printf("Elements after insertion\n"); for(i=0;i<6;i++) { printf("%d ",a[i]); } getch(); // Linux user - Remove this return 0; }
4. Program to delete element from an array. ( DELETION )
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring variable "i" to iterate loop int i; // Declaring variable pos=postion from where we want to delete element int pos; // Declaring and initialising array int a[5]={2,4,6,8,10}; // Printing values in array before deletion printf("Elements before deletion\n"); for(i=0;i<5;i++) { printf("%d ",a[i]); } printf("\n"); // Inputting position printf("Enter the position of element for deletion\n"); scanf("%d",&pos); // Performing deletion for(i=pos-1;i<5;i++) { a[i]=a[i+1]; } // Printing elements after deletion printf("Elements after deletion\n"); for(i=0;i<4;i++) { printf("%d ",a[i]); } getch(); // Linux user - Remove this return 0; }
5. Program to perform Transpose of a Matrix
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring variable i,j to iterate loop int i,j; // Declaring variable a to store matrix element int a[2][2]; // Inserting element in Matrix printf("Enter 2X2 matrix\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf("%d",&a[i][j]); } } // Displaying Matrix for(i=0;i<2;i++) { printf("\n"); for(j=0;j<2;j++) { printf("%d ",a[i][j]); } } // Performing transpose of Matrix and displaying transposed Matrix printf("\n"); printf("Transpose of matrix is \n"); for(i=0;i<2;i++) { printf("\n"); for(j=0;j<2;j++) { printf("%d ",a[j][i]); } } getch(); // Linux user - Remove this return 0; }