answersLogoWhite

0

What is a program in c to calculate factorial of number?

Updated: 10/2/2023
User Avatar

Wiki User

15y ago

Best Answer

First of all we will define what factorial is and how to it is calculated.

Factional is non negative integer. Notation would be n! It is calculated by multiplying all integers from 1 to n;

For example:

5! = 1 x 2 x 3 x 4 x 5 = 120.

Note: 0! = 1

Small C program that illustrates how factorial might be counted:

#include

int factorial(int num);

int main() {

int num;

printf("Enter number: ");

scanf("%d", &num);

printf("Factorial: %d\n", factorial(num));

return 0;

}

int factorial(int num) {

if (num == 0) {

return 1;

}

return num * factorial(num - 1);

}

Testing:

Enter number: 5

Factorial: 120

Enter number: 0

Factorial: 1

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a program in c to calculate factorial of number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

1.Write a c program for Fibonacci series?

/*program to calculate factorial of a number*/ #include<stdio.h> #include<conio.h> void main() { long int n; int a=1; clrscr(); printf("enter the number="); scanf("%ld",&n); while(n>0) { a*=n; n--; } printf("the factorial is %ld",a); getch(); }


Write a C-like program fragment that calculate the factorial function for argment 12 with do while loop?

#!/usr/bin/perl print factorial($ARGV[11]); sub factorial { my($num) = @_; if($num == 1) { return 1; # stop at 1, factorial doesn't multiply times zero } else { return $num * factorial($num - 1); # call factorial function recursively } }


7 Write a C program to compute the factorial of a number using for loop?

int factorial(int n) { int i; int f=1; for(i=2;i<=n;++i) f*=i; return f; }


What is the logic and theory for pascal triangle program in C?

long factorial(int); int main() { int i, n, c; printf("Enter the number of rows you wish to see in pascal triangle\n"); scanf("%d",&n); for ( i = 0 ; i < n ; i++ ) { for ( c = 0 ; c


Program to find the factorial of a number using recursion?

/*program to find the factorial of a given number*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; int fact(int); void main() { int n,c; printf("\n enter the number for which you want to find the factorial"); scanf("%d",&amp;n); c=fact(n); printf("\n the factorial of the number %d is %d",n,fact); getch(); } int fact(int n) { int k; if(n==0) return(1); else k=n*fact(n-1); return(k); }


How do you create factorial program in qbasic?

since factorial is for example , the factorial of 5 = 5 (5-1)(5-2)(5-3)(5-4) that means the last number to subtract from 5 is 4 , which is (n-1) ie the factorial of any number is (n-0)(.............)(n-(n-1)) to write this , 5 REM to calculate the factorial of any number 6 DIM fac AS INTEGER LET fac = 1 10 INPUT "enter the number to find its factorial "; a ' variable a 15 FOR b = 0 TO (a-1) 'numbers that will be subtracted from the " a" 20 c= a -b 'each number in the factorial calculation 25 fac = fac * c 'to compute each multiplication in the factorial 30 NEXT b 35 PRINT 'to leave a line 40 PRINT fac 45 END note this due to some unattained raesons works for numbers 0 to 7


C program to find factorial of a no?

this is a code for calculating it recursivelly: float Factorial (float n) { if (n&lt;=1) return 1.0; else return n* Factorial(n-1); }


Program for calculating factorial no using recursion in c?

factorial n is given by formula n! = n.(n-1)....1 int i; long x; x =1; for (i=n;i&gt;1;i--) x = x*i ; will calculate factorial. I have put x as long to avoid integer overflow. checks for n is positive etc. to be added.


What is the benefit of using function.llustrate different ways of passing argument to function in c plus plus?

Functions are used to reduce the lines of code and the complexity of the code. For an instance let us suppose that you want to calculate the factorial of numbers at different times in a program. There are two ways to do this 1. Write a 4-5 line code every time you want to calculate factorial. 2. Write a function of 4-5 lines which calculates the factorial and call that function every time you need to calculate factorial by just writing a single line. In C++ you can pass the variable, address of the variable or a reference to the variable in a function


How many keywords are there in c?

answer:32 programme to print factorial of a given number in c languages


What is c program to calculate product of all even numbers from entered number down to 1?

c is programming laungage


Program in c to generate table of factorials?

#include #include using std::cin;using std::cout;using std::endl;using std::tolower;long factorial(int N);int main(){int N = 0; //factorial of Nchar command = 'n';do{cout > N;cout