Solved programming problem

Basic Recursion problem in C.(add value)

Source code

#include <stdio.h>

int add(int n)

{

    if(n==1)

    {

        return 1; ///base case

    }

    else

    {

        return n+add(n-1); ///calling the same function again and again

    }

}

int main()

{

    int n;

    printf("Please Enter The value: ");

    scanf("%d",&n);

    add(n);

    printf("
 After add: %d
",add(n));

    return 0;

}

Output

Please Enter The value: 10

 After add: 55

Join the discussion

Add your comment