Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Recursive Multiplication Write a recursive function that accepts two argument

ID: 3825282 • Letter: 1

Question

1. Recursive Multiplication

Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows:

2. Recursive findings

5×6=6+6+6+6+6

Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in the array. Demonstrate the method in a program.

3. String Reverse

Write a recursive method that accepts a string as its argument and prints the string in reverse order. Demonstrate the method in a program.

4. maxElement Method

Write a method named maxElement, which returns the largest value in an array that is passed as an argument. The method should use recursion to find the largest element. Demonstrate the method in a program.

5. Recursive Power Method

Write a method that uses recursion to raise a number to a power. The method should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the method in a program.

6. Sum of Numbers

Write a method that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the method will return the sum of 1, 2, 3, 4, . . . 50. Use recursion to calculate the sum. Demonstrate the method in a program.

Explanation / Answer

After writing the code in c language:-

1.Recursive Multiplication:-

#include <stdio.h>

int multiplication(int, int);

int main()

{

int x,y, result;

printf("Enter two numbers: ");

scanf("%d%d", &x, &y);

result = multiplication(x, y);

printf("Multiplication of %d and %d is %d ", x, y, result);

return 0;
}

int multiplication(int x, int y)

{
if (x< y)
{
return multiplication(y, x);
}
else if (y != 0)
{
return (x + multiplication(x, y - 1));
}
else
{
return 0;
}
}

3. String Reverse:-

# include <stdio.h>

void reverseString(char *str)

{

if (*str)

{

            reverseString (str+1);

            printf("%c", *str);

}

}

int main()

{

char a[] = "String for test";

reverseString (a);

return 0;

}

4. maxElement Method:-

#include <stdio.h>

int maxElement(int[], int, int);

int main()

{

    int size;

    int largest;

    int a[20];

    int i;

printf("Enter size of the array:");

    scanf("%d", &size);

    printf("Printing the array: ");

    for (i = 0; i < size ; i++)

    {

        a[i] = rand() % size;

        printf("%d ", a[i]);

    }

    if (size == 0)

    {

        printf("Empty array ");

    }

    else

    {

        largest = a[0];

        largest = maxElement(a, size - 1, largest);

        printf(" The largest number in the array is: %d ", largest);

    }

}

int maxElement(int a[], int size, int largest)

{

    if (size == 1)

        return largest;

if (size > -1)

    {

        if (a[size] > largest)

        {

            largest = a[size];

        }

        return(largest = maxElement(a, size - 1, largest));

    }

    else

    {

        return largest;

    }

}

5. Recursive Power Method:-

#include <stdio.h>

long powerNumber (int, int);

int main()

{

    int pow, number;

    long result;

printf("Enter a number: ");

    scanf("%d", &number);

    printf("Enter it's power: ");

    scanf("%d", &pow);

  result = powerNumber (number, pow);

    printf("%d^%d is %ld", number, pow, result);

    return 0;

}

long powerNumber (int number, int pow)

{

    if (pow)

    {

        return (number * powerNumber (number, pow - 1));

    }

    return 1;

}

6. Sum of Numbers:-

#include <stdio.h>

int sumNumbers(int n);

int main()

{

    int num;

    printf("Enter a positive integer: ");

    scanf("%d", &num);

    printf("Sum = %d",sumNumbers(num));

    return 0;

}

int sumNumbers(int n)

{

    if(n != 0)

        return n + sumNumbers(n-1);

    else

        return n;

}

2. Recursive findings:-

#include<stdio.h>

int recFinding(int arr[], int l, int r, int a)

{

     if (r < l)

        return -1;

     if (arr[l] == a)

        return l;

     return recFinding(arr, l+1, r, a);

}

int main()

{

    int arr[] = {12, 34, 54, 2, 3}, i;

    int n = sizeof(arr)/sizeof(arr[0]);

    int a = 3;

    int index = recFinding(arr, 0, n-1, a);

    if (index != -1)

       printf("Element %d is present at index %d", a, index);

    else

        printf("Element %d is not present", a);

    return 0;

}