answersLogoWhite

0

#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);

}

}

User Avatar

Wiki User

16y ago

What else can I help you with?

Continue Learning about Engineering

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 } }


Write a FORTRAN code to calculating factorials?

Here is a simple FORTRAN code to calculate the factorial of a given non-negative integer: program factorial implicit none integer :: n, result print *, &quot;Enter a non-negative integer:&quot; read *, n result = 1 if (n &lt; 0) then print *, &quot;Factorial is not defined for negative numbers.&quot; else do i = 1, n result = result * i end do print *, &quot;Factorial of&quot;, n, &quot;is&quot;, result end if end program factorial This program prompts the user for an integer, checks if it's non-negative, and then calculates the factorial using a loop.


How do you write a program that calculate factorial in javascript?

factorial number Var num= prompt(&quot;enter any number &quot;); Var i=1; Var fact=1; for(i=1;i


Write a java program to print the last digit in Fibonacci series?

Just generate the Fibonacci numbers one by one, and print each number's last digit ie number%10.


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

Related Questions

1.Write a c program for Fibonacci series?

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


What is the flowchart and pseudocode for a program that accept and displays the factorial of a number?

A flowchart for a program that accepts and displays the factorial of a number would include the following steps: Start, Input the number, Initialize a variable for the factorial, Use a loop to calculate the factorial by multiplying the variable by each integer up to the number, Output the result, and End. Pseudocode for the same program would look like this: START INPUT number factorial = 1 FOR i FROM 1 TO number DO factorial = factorial * i END FOR OUTPUT factorial END


Program to generate Fibonacci series using storage class in c plus plus?

i dn't know. haha


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 } }


Write a FORTRAN code to calculating factorials?

Here is a simple FORTRAN code to calculate the factorial of a given non-negative integer: program factorial implicit none integer :: n, result print *, &quot;Enter a non-negative integer:&quot; read *, n result = 1 if (n &lt; 0) then print *, &quot;Factorial is not defined for negative numbers.&quot; else do i = 1, n result = result * i end do print *, &quot;Factorial of&quot;, n, &quot;is&quot;, result end if end program factorial This program prompts the user for an integer, checks if it's non-negative, and then calculates the factorial using a loop.


How do you write a program that calculate factorial in javascript?

factorial number Var num= prompt(&quot;enter any number &quot;); Var i=1; Var fact=1; for(i=1;i


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


A program for simple factorial in prolog?

In Prolog, a simple factorial program can be defined using recursion. Here's a basic implementation: factorial(0, 1). % Base case: factorial of 0 is 1 factorial(N, Result) :- N &gt; 0, N1 is N - 1, factorial(N1, Result1), Result is N * Result1. % Recursive case You can query the factorial of a number by calling factorial(N, Result). where N is the number you want to compute the factorial for.


Create a c plus plus program to generate Fibonacci series?

#include #include void main() { clrscr() int a=0,b=1,c,i,n; coutn cout


Write a java program to find the factorial of a given number?

Here's a simple Java program to find the factorial of a given number using a recursive method: import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&quot;Enter a number: &quot;); int number = scanner.nextInt(); System.out.println(&quot;Factorial of &quot; + number + &quot; is &quot; + factorial(number)); } static int factorial(int n) { return (n == 0) ? 1 : n * factorial(n - 1); } } This program prompts the user for a number and calculates its factorial recursively.


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 a program in c to calculate factorial of number?

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