#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int stack[10],n=0;
int fact(int);
int pop();
void push(int);
main()
{
int num;
printf("\n FACTORIAL USING STACK IMPLEMENTATED WITH ARRAY\n");
printf("\n Enter a number whose factorial is to be found(1-9): ");
scanf("%d",&num);
printf("\n The factorial is : %d",fact(num));
getch();
} void display()
{
int i;
printf("\n The stack is :\n");
for(i=1;i<=n;i++)
{
printf("%d\t",stack[i]);
}
}
int pop()
{ if(n==0)
{
printf("\n Stack empty!Pop not possible!");
exit(1);
}
else
{
int res=stack[n];
--n;
display();
return res;
}
} void push(int x)
{
if(n==9)
{
printf("\n Stack full!Push not possible");
exit(1);
}
else
{
n++;
stack[n]=x;
display();
}
}
int fact(int num)
{
if(num==2)
{
push(2);
push(1);
int res=1;
display();
while(n>=1)
res*=pop();
return res;
} else
{
push(num);
return fact(num-1);
}
}
function factorial(n) { var x=1; while(n>1) x*=(n--); return x; }
#!/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 } }
Just generate the Fibonacci numbers one by one, and print each number's last digit ie number%10.
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
#include #include void main(void) { int i,j,k,n; clrscr(); i=0; j=1; printf("%d %d ",i,j); for(n=0;n<=5;n++) { k=i+j; i=j; j=k; printf("%d ",k); } getch(); }
/*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(); }
i dn't know. haha
function factorial(n) { var x=1; while(n>1) x*=(n--); return x; }
#!/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 } }
#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
#include #include void main() { clrscr() int a=0,b=1,c,i,n; coutn cout
factorial n is given by formula n! = n.(n-1)....1 int i; long x; x =1; for (i=n;i>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.
Just generate the Fibonacci numbers one by one, and print each number's last digit ie number%10.
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
this is a code for calculating it recursivelly: float Factorial (float n) { if (n<=1) return 1.0; else return n* Factorial(n-1); }
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
kjhk