Q:

Write a progran to calculate factorial of a number

0

Write a progran to  calculate factorial of a number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Flowchart:

Algorithm:-

·        start

·        read n

·        if(n>0) then f=1

·        if(n==1) then f=f*n and n=n+1

·        write fact -stop

·        else error

·        stop

Program:-

#include<stdio.h>
#include<conio.h>
long int facto(int n)
{
if(n==1)
{
return 1;
}
else return n*facto(n-1);
}
void main()
{
long  int f;
int num;
printf("Enter any number :");
scanf("%d",&num);
if(num>0)
{
f=facto(num);
printf("factorial is %d",f);
}
else
{
	printf("\n error:given number is  %d negative",num);
}
}

Output:

Enter any number :4
factorial is 24

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Programming Exercises With Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a program in c to calculate power using recu... >>
<< Write a program in c to find the leap year...