
November 4th, 2012, 07:17 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 13 m 57 sec
Reputation Power: 0
|
|
|
Help on recursion
Hey all, I can't seem to figure out my recursion assignment. I'm able to compile but the program does not return the right answer. Could someone please enlighten me on it? Thank you.
18 int main(void) {
19 int temp, i, start, end, size =0, count =0, num[MAX_SIZE] = {0};
20
21 printf("Enter start and end: ");
22 scanf("%d %d", &start, &end);
23
24 while(start <= end) {
25 temp = start;
26 for(i=0;temp>0;i++) {
27 num[i] = temp % 10;
28 temp /= 10;
29 size++;
30 }
31 count += isPalindrome(num, size);
32 start++;
33 }
34
35 printf("Number of palindrome numbers = %d\n", count);
36
37 return 0;
38 }
39
40 // Return 1 if arr is a palindrome, or 0 otherwise.
41 // Driver function to call recursive function palindromeRecur()
42 int isPalindrome(int arr[], int size) {
43 int i=0;
44 return palindromeRecur(arr, i, size);
45 }
46
47 // Return 1 if arr is a palindrome, or 0 otherwise.
48 // Recursive function
49 int palindromeRecur(int arr[], int i, int size) {
50 if(size <= i)
51 return 1;
52
53 if(arr[i] != arr[size])
54 return 0;
55 else return palindromeRecur(arr, i+1, size-1);
56 }
|