diff --git a/Recursion/Sum_upto_n.c b/Recursion/Sum_upto_n.c new file mode 100644 index 0000000..4105cdf --- /dev/null +++ b/Recursion/Sum_upto_n.c @@ -0,0 +1,32 @@ +#include + +int sumOfRange(int); + +int main() +{ + int n1; + int sum; + printf("\n\n Recursion : calculate the sum of numbers from 1 to n :\n"); + printf("-----------------------------------------------------------\n"); + + printf(" Input the last number of the range starting from 1 : "); + scanf("%d", &n1); + + sum = sumOfRange(n1); + printf("\n The sum of numbers from 1 to %d : %d\n\n", n1, sum); + + return (0); +} + +int sumOfRange(int n1) +{ + int res; + if (n1 == 1) + { + return (1); + } else + { + res = n1 + sumOfRange(n1 - 1); //calling the function sumOfRange itself + } + return (res); +}