Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

recursive in c

#include <stdio.h>
int sum(int n);

int main() {
    int number, result;

    printf("Enter a positive integer: ");
    scanf("%d", &number);

    result = sum(number);

    printf("sum = %d", result);
    return 0;
}

int sum(int n) {
    if (n != 0)
        // sum() function calls itself
        return n + sum(n-1); 
    else
        return n;
}
Source by www.programiz.com #
 
PREVIOUS NEXT
Tagged: #recursive
ADD COMMENT
Topic
Name
6+6 =