WAP to Sum of numbers up to n term

Write a program to Sum of numbers up to nth term

#include<stdio.h>

int sum(int);

int main() {
	int n,s;
	printf("Enter any number: ");
	scanf("%d",&n);
	s=sum(n);
	printf("Sum = %d",s);
	return 0;
}

int sum(int n) {
	if(n!=0)
		return n+sum(n-1);
	else
		return n;
}

Output

Enter any number: 5
Sum = 15

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments