Friday, 20 November 2015

Even Odd Program using Stack

How to find  Even And Odd NO's Using Stack.?

Example code:

#include<iostream>
using namespace std;
class stack
{    private:
  int A[10],top;
 public:
 public:
   stack()                   //constructor
  {
top=-1;
  }  
 void push()               //fun to push

{
    if(top==10-1)
{  cout<<"Stack is full:";
return;
}
            else{

           int x;
           for( x=0;x<10;x++)
           {cout<<"Enter NO:"<<x+1<<":";
            cin>>A[x];}

            for( x=0;x<10;x++)
            {
             if(A[x]%2==0)
            {
            cout<<"\n even:"<<A[x]<<endl;} 
          else 
           cout<<"odd:"<<A[x];
           }
           
A[top++]=x;
}
}
void pop()              //fun to pop value
{
if(top==-1)
{
cout<<"Stack is Empty:"<<endl;
return;
}
else
{
    A[top--];
cout<<"Stack is deleted:"<<A[10];
}
}
};
int main()
{
int a;
start:
cout<<"\n  press 1   press to push: \n 2 to pop:"; 
cin>>a;

switch(a)
{
case 1:
s.push();
goto start;
break;
case 2:
s.pop();
goto start;
break;
default:
cout<<"Thanks:";
}
return 0;
}

No comments:

Post a Comment