In this C ++ example, the sum of the numbers in an interval is calculated. Firstly program takes two numbers from the user. By passing the received numbers to the recursive function, the numbers in the range are collected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> #include <stdlib.h> int sum(int min,int max) { if(max<=min) return min; else return max+sum(min,max-1); } int main() { int n1,n2; printf("Enter min number:"); scanf("%d",&n1); printf("Enter max number:"); scanf("%d",&n2); printf("Sum of numbers between %d and %d:%d",n1,n2,sum(n1,n2)); return 0; } |