0% found this document useful (0 votes)
7 views

Exercise 5 - Functions

The document discusses a C programming exercise to calculate the factorial of a given integer number using both recursive and non-recursive functions. It provides sample non-recursive and recursive code to calculate the factorial and expected outputs for an input number.

Uploaded by

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

Exercise 5 - Functions

The document discusses a C programming exercise to calculate the factorial of a given integer number using both recursive and non-recursive functions. It provides sample non-recursive and recursive code to calculate the factorial and expected outputs for an input number.

Uploaded by

pavanmay227597
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Programming in C – Practical Exercise 5

Exercise 5 Functions
Write a C program to find the factorial of a given integer number using
both recursive and non-recursive functions.

Objective:
The objective of this program is to:
Calculate the factorial of integer number using recursive and non-recursive
functions.

Program Code:

Non Recursive:
#include<stdio.h>
#include<conio.h>

void main()
{
intn,i,f;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
f=1;
for(i=1;i<=n;i++)
f=f*i;
printf("Factorial of %d = %d",n,f);
getch();
}

Expected Output:
Enter the number: 4
Factorial of 4 = 24

Sikkim Manipal University B2111 Page No.: 7


Programming in C – Practical Exercise 5

Recursive:
longint fact(int n)
main( )
{
int n;
clrscr( );
printf(“\n enter an integer no:”);
scanf(“%d”,&n);
printf(“ factorial of %d = %d \n”, n, fact(n));
getch( );
}

longint fact(int n)
{
if(n==0)
return 1;
else
return n*fact(n-1);
}

Expected Output

Recursive
enteraninteger no: 9
factorial of 9 = 362880

Sikkim Manipal University B2111 Page No.: 8

You might also like