Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Write a recursive function sumRecursive() to find the sum of first n natural numbers.

Let us say S(n) is sum of first n natural numbers. It can be defined as a mathematical recursive formula as follows:
S(n) = 1 if (n == 1) (Because 1 is the first natural number)
S(n) = n + S(n - 1) (Sum of first n natural numbers is n + Sum of first n - 1 natural numbers)



==============================================================================
Compilation Errors

warning in SumOfNaturalNum1.c, Line No : 1
 S(n) = n + S(n - 1);
 ^
 data definition has no type or storage class


warning in SumOfNaturalNum1.c, Line No : 1
 type defaults to 'int' in declaration of 'S' [-Wimplicit-int]


warning in SumOfNaturalNum1.c, Line No : 1
 parameter names (without types) in function declaration


Error in SumOfNaturalNum1.c, Line No : 1
 function 'S' is initialized like a variable


Error in SumOfNaturalNum1.c, Line No : 1
 S(n) = n + S(n - 1);
        ^

============================================================================

What I have tried:

Correct/Complete the Code :


#include <stdio.h>
#include "SumOfNaturalNum1.c" //anyone please tell me the logic to write in "SumOfNaturalNum1.c"
void main() {
	int n;
	printf("Enter a natural number : ");
	scanf("%d",&n);
	if (n > 0)
		printf("The sum of first %d natural numbers : %d\n", n, sumRecursive(n));
	else
		printf("Invalid Number\n");
}
Posted
Updated 28-Mar-21 23:04pm
v4
Comments
Stefan_Lang 18-Jan-19 6:31am    
You did not show the code that is actually causing the problems: the code contained in the file "SumOfNaturalNum1.c".

The code you showed has only one issue. that you are including a C file!

First let's deal with the "compilation errors": You can't just copy your homework question into a .C source file and compile it - that will never, ever work! The question is am English description of what your teacher requires the code to do, it is up to you to read the instructions and write code to perform that function. So start by removing the "SumOfNaturalNum1.c" #include, and add a function signature to your main.c code:
C++
#include <stdio.h>

int sumRecursive(int n);

void main() 
    {
    int n;
    printf("Enter a natural number : ");
    scanf("%d",&n);
    if (n > 0)
    	printf("The sum of first %d natural numbers : %d\n", n, sumRecursive(n));
    else
    	printf("Invalid Number\n");
    }
Then create the function itself at the end of the file:
C++
int sumRecursive(int n)
    {
    ...
    return result;
    }
Now all you have to do is fill in the function by following the instructions you have been given.
 
Share this answer
 
Comments
CPallini 18-Jan-19 3:25am    
Why on earth did you exclude 0? :-D
(I see it is blindly excluded by the OP teacher).
OriginalGriff 18-Jan-19 3:36am    
Probably because it depends which definition you use!
https://en.wikipedia.org/wiki/Natural_number
CPallini 18-Jan-19 4:09am    
I know, but that is not fair :-)
By the way, Wirth blamed the factorial classic exercise as 'poor example' of recursion. I am curious to know what he think about THIS exercise.
OriginalGriff 18-Jan-19 4:15am    
Planning on starting a SPCZ?

(Society for the Prevention of Cruelty to Zeros)
CPallini 18-Jan-19 5:31am    
No need for such a thing. Zero takes its revenges.
The logic of the recursive (say) sum function is already given in the exercise text. Just the implementation is missing.
Now what is exactly your problem with such implementation?
If you are not able to write a recursive function, then have a look at this page: C++ Recursion - Recursive Function[^].
On the other hand, if you are not able to write a function at all, then have a look at a career in Project Management.
 
Share this answer
 
Comments
OriginalGriff 18-Jan-19 3:42am    
Ouch! Dat Naaaasty! :laugh:
Maciej Los 18-Jan-19 3:55am    
5ed!
CPallini 18-Jan-19 5:32am    
Thanks!
CHill60 18-Jan-19 6:43am    
"then have a look at a career in Project Management. " :still laughing:
Quote:
Compilation Errors

You forgot to show us the source code of SumOfNaturalNum1.c, so nobody canhelp to fix what you have done.
Quote:
Correct/Complete the Code

Since the problem have an easy solution with a multiplication and a division, the intend is to make you work/train on recursion. If we just do it for you, it is a bad service we give you because you learn nothing, it defeat the purpose of homework.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
 
Share this answer
 
#include <stdio.h>
#include "SumOfNaturalNum1.c"
void main() {
	int n;
	printf("Enter a natural number : ");
	scanf("%d",&n);
	if (n > 0)
		printf("The sum of first %d natural numbers : %d\n", n, sumRecursive(n));
	else
		printf("Invalid Number\n");
}






finally, i got the answer in...
"SumOfNaturalNum1.c

==================================================================
int sumRecursive(int number)
{
	if(number==0)
	return 0;
	else
	return number+sumRecursive(number-1);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900