0% found this document useful (0 votes)
10 views8 pages

Assignment 9

Uploaded by

Priyanka rani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Assignment 9

Uploaded by

Priyanka rani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

ASSIGNMENT 9

Q1. Write a program to find factorial of a number using recursion.

#include<stdio.h>

int multiplyNumbers(int n);

int main() {

int n;

printf("Enter a positive integer: ");

scanf("%d",&n);

printf("Factorial of %d = %ld", n, multiplyNumbers(n));

return 0;

int multiplyNumbers(int n) {

if (n>=1)

return n*multiplyNumbers(n-1);

else

return 1;

Q2. Write a program to generate Fibonacci series using recursion.

#include<stdio.h>

int fibonacci(int);

int main(void)

int terms;

printf("Enter terms: ");

scanf("%d", &terms);
for(int n = 0; n < terms; n++)

printf("%d ", fibonacci(n));

return 0;

int fibonacci(int num)

if(num == 0 || num == 1)

return num;

else

return fibonacci(num-1) + fibonacci(num-2);

Q3. Write a program to find sum of array elements using recursion.

#include <stdio.h>

int findSum(int A[], int N)

if (N <= 0)

return 0;
return (findSum(A, N - 1) + A[N - 1]);

int main()

int A[] = { 1, 2, 3, 4, 5,6 };

int N = sizeof(A) / sizeof(A[0]);

printf("%d", findSum(A, N));

return 0;

Q4. Write a program to search an element in an array using recursion

1. #include <stdio.h>

2. int RecursiveLS(int arr[], int value, int index, int n)

3. {

4. int pos = 0;

5.

6. if(index >= n)

7. {

8. return 0;

9. }

10.

11. else if (arr[index] == value)

12. {

13. pos = index + 1;

14. return pos;

15. }

16.
17. else

18. {

19. return RecursiveLS(arr, value, index+1, n);

20. }

21. return pos;

22. }

23.

24. int main()

25. {

26. int n, value, pos, m = 0, arr[100];

27. printf("Enter the total elements in the array ");

28. scanf("%d", &n);

29.

30. printf("Enter the array elements\n");

31. for (int i = 0; i < n; i++)

32. {

33. scanf("%d", &arr[i]);

34. }

35.

36. printf("Enter the element to search ");

37. scanf("%d", &value);

38.

39. pos = RecursiveLS(arr, value, 0, n);

40. if (pos != 0)

41. {

42. printf("Element found at pos %d ", pos);

43. }

44. else

45. {

46. printf("Element not found");

47. }
48. return 0;

49. }

Q5. Write a program to perform following operations on a string input by user: a) check if it is
palindrome or not b) find duplicate elements c) compare it with another string

a) check if it is palindrome or not

#include <stdio.h>

#include <string.h>

int main() {

char str[100];

int length, i, flag = 0;

printf("Enter a string: ");

scanf("%s", str);

length = strlen(str);

for (i = 0; i < length / 2; i++) {

if (str[i] != str[length - i - 1]) {

flag = 1;

break;

if (flag) {

printf("%s is not a palindrome\n", str);

} else {
printf("%s is a palindrome\n", str);

return 0;

b) Find duplicate elements in a string

#include <stdio.h>

#include <string.h>

int main() {

char str[100];

int count[256] = {0}; // Assuming ASCII character set

int i;

printf("Enter a string: ");

scanf("%s", str);

for (i = 0; i < strlen(str); i++) {

count[(int)str[i]]++;

printf("Duplicate characters in the string are:\n");

for (i = 0; i < 256; i++) {

if (count[i] > 1) {

printf("%c occurs %d times\n", i, count[i]);

}
return 0;

c) Compare it with another string

#include <stdio.h>

#include <string.h>

int main() {

char str1[100], str2[100];

int result;

printf("Enter the first string: ");

scanf("%s", str1);

printf("Enter the second string: ");

scanf("%s", str2);

result = strcmp(str1, str2);

if (result == 0) {

printf("The strings are equal.\n");

} else if (result < 0) {

printf("The first string is less than the second string.\n");

} else {

printf("The first string is greater than the second string.\n");

}
return 0;

You might also like