Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Sum_upto_n.c #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Recursion/Sum_upto_n.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<stdio.h>

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);
}