0% found this document useful (0 votes)
6 views1 page

Recursive Factorial Program in C

The document provides a C program that calculates the factorial of a number using recursion. It includes a base case for 0 and 1, and handles negative input by displaying an error message. The program prompts the user for a number and outputs the factorial if the input is non-negative.

Uploaded by

mraman2495
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Recursive Factorial Program in C

The document provides a C program that calculates the factorial of a number using recursion. It includes a base case for 0 and 1, and handles negative input by displaying an error message. The program prompts the user for a number and outputs the factorial if the input is non-negative.

Uploaded by

mraman2495
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q.1 Write a program of factorial using recursion.

Ans:-
#include <stdio.h>
#include<conio.h>
Int factorial(int n)
{
if (n == 0 || n == 1)
{
return 1; // Base case
} else
{
return n * factorial(n - 1);
}
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial is not defined for negative numbers.\n");
}
Else
{
printf("Factorial of %d is %d\n", num, factorial(num));
}
return 0;
}

You might also like