function fact(int n); ans = n * fact(n-1); endfunction
#include<stdio.h> #include<conio.h> void main() { int i,n,fact=1; clrscr(); printf("enter the number"); scanf("%d",&n); for(i=1;i<=n;i++) printf("%d",n); fact=fact*i; { printf("the factorial is=%d",fact); } getch(); } By:-Abhishek Goyal(goyal.abhi40@yahoo.com)
' Iterative solution Function iterativeFactorial(ByVal n As Long) As Long Dim factorial As Long = 1 For i As Long = 1 To n factorial *= i Next Return factorial End Function ' Recursive solution Function recursiveFactorial(ByVal n As Long) As Long If n <= 1 Then Return n End If Return n * recursiveFactorial(n - 1) End Function
/* gcc -ansi -Wall -Wextra -pedantic -s -static 0.c -o 0 */ #include <stdio.h> int main ( ) { int n = 5 , factorial = 1 ; while ( n != 0 ) { factorial *= n ; n -- ; } printf ( "%i\n" , factorial ) ; return 0; }
#!/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 } }
C Program to find factorial of a numberSimple C Programint main() {int n,i,f=1;printf("enter any num \n");scanf("%d",&n);for(i=1;i
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! = 1Small 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: 5Factorial: 120Enter number: 0Factorial: 1
I've been trying to figure this out myself for a discrete structure class.From the digging I've done online I've found this formula thus far:if n is the number of digits, and s is the sum:C(n+s-1 , n-1) where C denotes "choose" as in C(n , k) "n choose k" which can be solved by(n! / [ (n-k)! * k! ] )this seems to work for situations where the sum is < 10, or so claims the forum I found.I whipped it all up into a scheme function if anyone wants to take advantage:;;factorial: num -> num;;finds factorial of num.(define (factorial num)(if (or (= num 1) (= num 0))1(* num (factorial (sub1 num)))));;==========================;;xchy: num num -> num;;the "n choose k" function(define (xchy n k)(/ (factorial n)(* (factorial(- n k))(factorial k))));;==========================;;n-dig&sum-s: num num -> num;;finds the number of n digit combinations with sum s;;C(n+s-1,n-1)(define (n-dig&sum-s n s)(xchy (sub1 (+ n s))(sub1 n)))
echo "Enter a number: " read num i=2 res=1 if [ $num -ge 2 ] then while [ $i -le $num ] do res=`expr $res \* $i` i=`expr $i + 1` done fi echo "Factorial of $num = $res" v
#include<stdio.h> #include<conio.h> void main() { int i,n,fact=1; clrscr(); printf("enter the number"); scanf("%d",&n); for(i=1;i<=n;i++) printf("%d",n); fact=fact*i; { printf("the factorial is=%d",fact); } getch(); } By:-Abhishek Goyal(goyal.abhi40@yahoo.com)
' Iterative solution Function iterativeFactorial(ByVal n As Long) As Long Dim factorial As Long = 1 For i As Long = 1 To n factorial *= i Next Return factorial End Function ' Recursive solution Function recursiveFactorial(ByVal n As Long) As Long If n <= 1 Then Return n End If Return n * recursiveFactorial(n - 1) End Function
/* gcc -ansi -Wall -Wextra -pedantic -s -static 0.c -o 0 */ #include <stdio.h> int main ( ) { int n = 5 , factorial = 1 ; while ( n != 0 ) { factorial *= n ; n -- ; } printf ( "%i\n" , factorial ) ; return 0; }
#!/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 } }
C Program to find factorial of a numberSimple C Programint main() {int n,i,f=1;printf("enter any num \n");scanf("%d",&n);for(i=1;i
report zbharath. data:num type i value 5, fac type i value 0. perform fact using num changing fac. write:/ 'factorial of',num,'is',fac. form fact. using value(f-num) type i. changing f-fact type i. f-fact=1. while f-num ge 1. f-fact=f-fact*f-num. f-num=f-num-1. endwhile. endform.
factorial number Var num= prompt("enter any number "); Var i=1; Var fact=1; for(i=1;i
#include <stdio.h> int main() { int fact=1,num; printf("Enter number: "); scanf("%d",&num); if((num==0) (num==1)) { printf("Factorial = %d",fact); return 0; } else { while(num!=1) { fact=fact*num; num--; } printf("Factorial = %d",fact); return 0; } }
this is a code for calculating it recursivelly: float Factorial (float n) { if (n<=1) return 1.0; else return n* Factorial(n-1); }