determine the cumulative sum of n floating point numbers read a new number into the computer during each call to recursive function
Sample input:
n=3
Enter the number:0.8
Enter the number:0.1
Enter the number:0.2
Sample output:
Cumulative sum=1.1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//implement recusrive function summ
double recsum(int n)
{
if (n == 0)
{
//base recursion
double a;
printf("Enter the number: ");
scanf("%lf",&a);
return a;
}
double a;
printf("Enter the number: ");
scanf("%lf", &a);
return a + recsum(n - 1);
}
int main()
{
int n;
printf("n=");
scanf("%i", &n);
double sum = recsum(n-1);
printf("%0.1lf\n", sum);
return 0;
}
Comments
Leave a comment