Sum Numbers Using Recursion - C Function

C examples for Function:Recursive Function

Introduction

This program performs summation of numbers 1 to 4 using recursion.

Demo Code



#include <stdio.h>

int summation (int intM);

int main(){//from w  w  w.j a  v  a  2s. c  om
      int counter = 4, result;
      result = summation(counter);
      printf("Sum : 1 + 2 + 3 + 4 = %d\n", result);
      return(0);
}

int summation(int intM){
     if (intM == 1)
        return 1;
     else
        return (intM + summation(intM - 1));
}

Related Tutorials