Our Feeds

Wednesday, 7 October 2015

AJITH KP

Factorial using stack in C++

Hi GuyZzZ...
     I would like to share the code which you can implement factorial using stack. It is a question from Data Structures and Algorithm.


Source Code


#include <iostream>
using namespace std;
/*
 * Coded By Ajith Kp [ajithkp560]
 *  http://www.terminalcoders.blogspot.com
 *  
*/
class stack
{
 int arr[1024], ptr;
 public:
 stack()
 {
  ptr = 0;
 }
 void push(int n)
 {
  arr[ptr++]=n;
 }
 int pop()
 {
  return arr[--ptr];
 }
};
int main()
{
 int n, i;
 stack s;
 cout<<"Limit: ";
 cin>>n;
 for(i=n;i>0;i--)
 {
  s.push(i);
 }
 int fact = 1;
 while(n--)fact*=s.pop();
 cout<<"Factorial: "<<fact<<endl;
 return 0;
}