Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
45 views

Write A Program To Insert An Element in An Array

The document contains code snippets for several array programs: 1) A program to insert an element into an array by making space for the new element and shifting other elements over. 2) A program to reverse an array by swapping the first and last elements, second and second to last, and so on. 3) A program to search an array for a key element and return whether it was found. 4) A program to print Pascal's triangle by calculating factorials to determine the elements in each row. 5) A program to check if a string is a palindrome by comparing the first and last elements, second and second to last, and so on.

Uploaded by

Kanika Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Write A Program To Insert An Element in An Array

The document contains code snippets for several array programs: 1) A program to insert an element into an array by making space for the new element and shifting other elements over. 2) A program to reverse an array by swapping the first and last elements, second and second to last, and so on. 3) A program to search an array for a key element and return whether it was found. 4) A program to print Pascal's triangle by calculating factorials to determine the elements in each row. 5) A program to check if a string is a palindrome by comparing the first and last elements, second and second to last, and so on.

Uploaded by

Kanika Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Write a program to insert an element in an array

#include<stdio.h> #include <stdlib.h> int main() { int i,a,*p,index,key,temp; //how many elements you want to enter = a scanf("%d",&a); p=(int*)malloc((a+1)*sizeof(int)); for(i=0;i<a;i++) scanf("%d",&p[i]); //where do you want to insert an element = index scanf("%d",&index); //value of the element you want to insert = key scanf("%d",&key); for(i=a-1;i>=index;i--) { temp=p[i]; p[i+1]=temp; } p[index]=key; for(i=0;i<=a;i++) printf("%d ",p[i]); return 0; }

Write a program to reverse an array


#include<stdio.h> #include <stdlib.h> int main() { int i,a,*p,count; //how many elements you want to enter? = a scanf("%d",&a); p=(int*)malloc(a*sizeof(int)); for(i=0;i<a;i++) scanf("%d",&p[i]); for(i=0,count=a-1;i<=count;i++,count--) { p[i]=p[i]+p[count]; p[count]=p[i]-p[count]; p[i]=p[i]-p[count]; } for(i=0;i<a;i++) { printf("%d ",p[i]); } return 0; }

Write a program to find an element in an array


#include<stdio.h> #include <stdlib.h> int main() { int i,a,*p,key,flag=0; //how many elements you want to enter? = a scanf("%d",&a); p=(int*)malloc(a*sizeof(int)); for(i=0;i<a;i++) scanf("%d",&p[i]); //enter element scanf("%d",&key); for(i=0;i<a;i++) { if(key==p[i]) { } } if(flag==1) printf("fount at index %d",i+1); else printf("doesnt exist"); return 0; flag=1; break;

Write a program to print Pascals triangle


#include <stdio.h> long factorial(int n) { int c; long result = 1; for( c = 1 ; c <= n ; c++ ) result = result*c; return ( result ); } int main() { int i, n, c; scanf("%d",&n); printf("number of rows in pascal triangle = %d\n",n); for ( i = 0 ; i < n ; i++ ) { for ( c = 0 ; c <=(n-i-1) ; c++ ) printf(" "); for( c = 0 ; c <= i ; c++ ) printf("%ld ",factorial(i)/(factorial(c)*factorial(ic))); printf("\n"); } return 0; }

Write a program to find if a string is a palindrome


#include<stdio.h> int main() { char *p,c; int capacity=5,i=0,j,k,flag=0; p=(char*)malloc(capacity*sizeof(char)); while((c=getchar())!='\n') { p[i]=c; i++; if(i==capacity) { capacity=2*capacity; p=(char*)realloc(p, capacity*sizeof(char)); } } for(k=0,j=i-1;k<=j;k++,j--) if(p[k]!=p[j]) { flag=1; break; } if(flag==0) printf("PALINDROME"); else printf("NOT A PALINDROME"); return 0;}

You might also like