Saturday, January 28, 2017

Finding the LCM and GCD of multiple numbers in python


Finding GCD of Multiple numbers:

#!/usr/bin/python



# finding gcd for two numbers

def gcd(x, y):

    while y != 0:

        (x, y) = (y, x % y)

    return x
if __name__ == '__main__':
 numbers= []
 count = input("how many numbers")
 for i in range (0, count):
  number = input("enter the number %d  " %(i+1))
  numbers.append(number)
 sorted_num = sorted(numbers)
 gcd_val = sorted_num[0]

 for i in range(1,count):
  gcd_val = gcd(gcd_val,sorted_num[i])


print "gcd value= %d" %gcd_val
Finding LCM values

def lcm(x, y):
   """This function takes two
   integers and returns the L.C.M."""

   # choose the greater number
   if x > y:
       greater = x
   else:
       greater = y

   while(True):
       if((greater % x == 0) and (greater % y == 0)):
           lcm = greater
           break
       greater += 1

   return lcm


if __name__ == '__main__':
  numbers= []
  count = input("how many numbers")
  for i in range (0, count):
    number = input("enter the number %d  " %(i+1))
    numbers.append(number)
 
  lcm_val = numbers[0]

  for i in range(1,count):
    lcm_val = lcm(lcm_val,numbers[i])


print "lcm value= %d" %lcm_val



Sunday, January 8, 2017

Stack and Queue Implementation in C++

Implementation of Stack in C++

#include<iostream>

#include<cstdlib>

#define MAX_SIZE 20

using namespace std;

class Stack{

    private:

        int item[MAX_SIZE];

        int top;

    public:

        Stack(){

            top = -1;

        }

        void push(int data){

            item[++top] = data;

        }

        int pop(){

            return item[top--];

        }

        int size(){

            return top+1;

        }

        bool is_empty(){

            if(top==-1)

                return true;

            else

                return false;

        }

        bool is_full(){

            if(top==MAX_SIZE-1)

                return true;

            else

                return false;

        }

        void display(){

            for(int i=0; i<this->size(); i++){

                cout<<item[i]<<" ";

            }

            cout<<endl;

        }

};

int main(){

    int input;

    int temp;

    Stack stack;

    while(true){



        cout<<"Choose any one of the following"<<endl;

        cout<<"1. Push Item. "<<endl;

        cout<<"2. Pop Item."<<endl;



        cout<<"3. Display all elements of stack."<<endl;

        cout<<"4. Size of Stack."<<endl;

        cout<<"5. Exit."<<endl;

        cout<<"Your Choice: ";

        cin>>input;

        switch(input){

            case 1:

                if(!stack.is_full()){

                    cout<<"Enter number to push: ";

                    cin>>temp;

                    stack.push(temp);

                }else{

                    cout<<"Can't push. Stack is Full!!"<<endl;

                }

                break;

            case 2:

                if(!stack.is_empty()){

                    cout<<"The number popped is: "<<stack.pop()<<endl;

                }else{

                    cout<<"Cant't pop. Stack is Empty!!"<<endl;

                }

                break;



            case 3:

                if(!stack.is_empty()){

                    cout<<"The elements in stack are: ";

                    stack.display();

                    cout<<endl;

                }else{

                    cout<<"Stack is Empty"<<endl;

                }

                break;



            case 4:

                cout<<"The size of the stack is: "<<stack.size()<<endl;

                break;



            case 5:

                exit(0);

                break;

        }

    }

    return 0;

}
Implementation of Queue in C++



#include<iostream>

#define Maxsize 10

using namespace std;

class Queue

{



    private:

       int front;

       int rear;

       int queue[Maxsize];

    public:

       Queue()

       {

           front=0;

           rear=-1;

       }

       void enqueue()

       {

           int data;

           cout<<"Enter data to enqueue :";cin>>data;

           if(rear==Maxsize-1)

              cout<<"Queue is FULL .\n";

            else

              queue[++rear]=data;

       }

       int dequeue()

       {

           if(front>rear)

             cout<<"Queue is EMPTY .\n";

            else

              {

                  int data=queue[front];

                  return data;

              }

       }

       void display()

       {

           for(int i=0;i<=rear;i++)

             cout<<queue[i]<<endl;

       }



};

int main()

{

    Queue q;

    int res;

    char choice;

    cout<<"\n\tQueue Operation :\n\n";

    cout<<"\n1.Enqueue \n";

    cout<<"2.Dequeue \n";

    cout<<"3.Display\n";

    do{

    cout<<"Enter your choice :1/2/3   :: ";

    cin>>res;

    switch(res)

    {

        case 1:q.enqueue();break;

        case 2:q.dequeue();break;

        case 3:q.display();

    }

    cout<<"Want to continue ? ";cin>>choice;

    }while(choice=='y');

   return 0;

}