/* * To check if an array forms a palindrome or not using a recursive function * @author-Karan Narain(karan@iitk.ac.in) * */ #include /* * This program uses pointers.For a non-pointer implementation,replace the pointers by array indices * The parameters p and q point to the first and last elements of the array respectively */ void palirec(int *p,int *q) { if(p>=q) { printf("\nThe array forms a palindrome\n"); } else { if(*p==*q) palirec(p+1,q-1); else printf("\nThe array does not form a palindrome\n"); } } int main() { int A[10],i=0,temp,len,*p=NULL,*q=NULL; //len holds the number of elements in array A.It would be equal to the last value of i before the while loop ends or breaks while(i<10) { printf("Enter a positive element(To stop entering,enter a zero)\n"); scanf("%d",&temp); if(temp==0) { A[i]=temp; break; } else if(temp<0) continue; //continue will take the control back to while(i<10) for a new iteration,i.e. another element will be input else { A[i]=temp; i++; } } len=i; //if no element was entered if(len==0) printf("The array has no elements\n"); else { printf("The array you entered is :\n"); for(i=0;i