Recursive function in c language Reviewed by Momizat on . The function which is used to call itself inside its own body is known as recursive function and such also known as recursion. Input a number and print the sum The function which is used to call itself inside its own body is known as recursive function and such also known as recursion. Input a number and print the sum Rating: 0

Recursive function in c language

The function which is used to call itself inside its own body is known as recursive function and such also known as recursion.

Input a number and print the sum of all number from to the inputed number.

#include <stdio.h>
#include <conio.h>
main(){
	clrscr();
	int n,s;
	int sumrec(int);
	printf("Enter a number:");
	scanf("%d",&n);
	s=sumrec(n);
	printf("Sum is %d",s);
	getch();
}
int sumrec(int n){
	int s;
	if(n==1){
		return 1;
	}else{
		s=sumrec(n-1);
		return (n+s);
	}
}

Input a number and print factorial.

#include <stdio.h>
#include <conio.h>
main(){
	clrscr();
	int n,s;
	int factorial(int);
	printf("Enter a number:");
	scanf("%d",&n);
	s=factorial(n);
	printf("Factorial is %d",s);
	getch();
}
int factorial(int n){
	int s;
	if(n==1){
		return 1;
	}else{
		s=sumrec(n-1);
		return (n*s);
	}
}

Comments (1)

Leave a Comment

© 2014 Powered By