Write a recursive function that computes the sum of all numb

Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Write a recursive function that finds and returns the minimum element in an array, where the array and its size are given as parameters. Write a recursive function that computes and returns the sum of all elements in an array, where the array and its size are given as parameters.

Solution

sum 1 to N numbers using recursion

public class SumRecursion

{

public static void main(String[] args)

{

System.out.println(\"Enter N\");

try

{

BufferedReader br = new BufferedReader( new InputStreamReader(System.in));

int number= Integer.parseInt(br.readLine());

/* Scanner sc = new Scanner(System.in);

int number = sc.nextInt(); */

int sum = sumR(number);

System.out.println(\"Sum of \" + number + \" Numbers using Recursion is : \" + sum);

}

catch (Exception e)

{

System.out.println (\"Give Proper Input i.e. Enter Numeric value only and with in range\");

}

}

public static int sumR(int num)

{

int sum;

if(num==1) return(1) ;

else sum=num + sumR(num-1);

return sum;

}

}

ecursive function that finds and returns the minimum element in an array

#include <stdio.h>

int main()

{

    int array[100], minimum, size, c, location = 1;

    printf(\"Enter the number of elements in array\ \");

    scanf(\"%d\",&size);

    printf(\"Enter %d integers\ \", size);

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

    scanf(\"%d\", &array[c]);

    minimum = array[0];

    for ( c = 1 ; c < size ; c++ )

    {

        if ( array[c] < minimum )

        {

           minimum = array[c];

           location = c+1;

        }

    }

    printf(\"Minimum element is present at location %d and it\'s value is %d.\ \", location, minimum);

    return 0;

}

C Program to find sum of N numbers using recursion

#include <stdio.h>

#include <conio.h>

int getSum(int *inputArray, int lastIndex);

int main(){

    int inputArray[100], counter, numberCount;

    

    printf(\"Enter number of elements in Array: \");

    scanf(\"%d\", &numberCount);

    printf(\"Enter %d numbers \ \", numberCount);

    for(counter = 0; counter < numberCount; counter++){

        scanf(\"%d\", &inputArray[counter]);

    }

    

    printf(\"Sum of all numbers are : %d\",

        getSum(inputArray,numberCount - 1));

    getch();

    return 0;

}

/*

* getSum(array, index) = array[index] + getSum(array, index-1);

*/

int getSum(int *inputArray, int lastIndex){

    int mid;

    if(NULL == inputArray || lastIndex < 0)

        return 0;

    return inputArray[lastIndex] + getSum(inputArray, lastIndex -1);

}

 Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Write a recursive function that finds and returns t
 Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Write a recursive function that finds and returns t
 Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Write a recursive function that finds and returns t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site