0% found this document useful (0 votes)
16 views2 pages

Recursive Code for OH Bonds and Sum

The document contains two programming tasks involving recursion in C. The first task is to count the number of OH bonds in a user-provided chemical formula, while the second task sums the elements of an array using recursion. Both tasks include code implementations and user input prompts.
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)
16 views2 pages

Recursive Code for OH Bonds and Sum

The document contains two programming tasks involving recursion in C. The first task is to count the number of OH bonds in a user-provided chemical formula, while the second task sums the elements of an array using recursion. Both tasks include code implementations and user input prompts.
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

QUES 1.

Write a code to determine number of OH bonds in the user given compound using
recursion. For example, input in form Mg(OH)2 as MGOHOH.

SOL.

#include<stdio.h>

#include<string.h>

int bond(int n, char a[], int count, int size) {

if (a[n] == 'O' && a[n + 1] == 'H') {

count++;

if (n < size - 2) {

return bond(n + 1, a, count, size);

} else {

return count;

int main() {

char a[100];

printf("enter the formula in capital letters:");

scanf("%s",a);

int c = strlen(a);

int count = bond(0, a, 0, c);

printf("Number of OH bonds: %d\n", count);

return 0;

OUTPUT:
QUES 2. Write a program to sum of array elements using recursion.

SOL.

#include<stdio.h>

int sum(int a[], int n) {

if(n == 0)

return 0;

else

return *a + sum(a+1,n-1);

int main() {

int n;

printf("Enter enumber of elements:");

scanf("%d",&n);

int a[n];

printf("Enter elements");

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

scanf("%d",&a[i]);

printf("Sum of elements = %d",sum(a,5));

You might also like