Factorial: its a mathematical function defined as 4!=4x3x2x1=120. Yes just like that. It will multiply all numbers before the given numbers till 1(Worst definition ever).
Method: calculating factorial is pretty straight forward. We will use a loop, which will loop from 1 to given number. store our result in a number that was initially 1 (not 0 ,cause 0 multiplied by anything will be zero).
Code in C:
Method: calculating factorial is pretty straight forward. We will use a loop, which will loop from 1 to given number. store our result in a number that was initially 1 (not 0 ,cause 0 multiplied by anything will be zero).
Code in C:
#include<stdio.h> int factorial(int x) { int result=1;//initialize to 1 int i; for(i=1;i<=x;i++) { result=result*i; } return(result); } int main() { int n; printf("\n Enter the number to calculate its factorial="); scanf("%d",&n); printf("\n Factorial of number =%d",factorial(n)); }
No comments:
Post a Comment