Answer to Question #218073 in C for Hemambar

Question #218073

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


1
Expert's answer
2021-07-20T15:29:02-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS