Friday, September 30, 2016

Operations On Queue Implemented Using Linklist :)

Tags

Program to Operate Queue Which Is Implemented Using Linklist:


#include<stdio.h>
#include<stdlib.h>  //for malloc function
struct node         //node in a link list
{
    int data;
    struct node* next;
}*head;
void insert(int num)  //for insertion
{   struct node *ptr=malloc(sizeof(struct node));
    struct node *temp=malloc(sizeof(struct node));
    temp->data=num;
    temp->next=NULL;
    if(head==NULL)   //for first insertion
    {
        head=temp;
    }
    else
    {
    ptr=head;
    while(ptr->next!=NULL)   //for pointing to last node
    {
        ptr=ptr->next;
    }
    ptr->next=temp;         //saving temp as last node

}}
void delete()     // for deletion
{
    if(head==NULL)  //no node in queue
    {
        printf("Queue is Empty");
    }
    else{
        head=head->next;    }
}
void display()   //for display
{
    struct node * ptr=head;
    if(head==NULL)
    {
        printf("\nQueue Is Empty\n");
        return;
    }
    printf("\nElements Are:\n");
    while(ptr!=NULL)
    {
        printf("%d\n",ptr->data);
        ptr=ptr->next;   //for moving forward
    }
}
main()
{ int choice,num;
    head=NULL; //initially head is null
    while(1)  //for user interface
    { printf("\nOperations On Queue\n");
         printf("Enter your choice\n");
              printf("1. INSERT \n");
              printf("2. DELETE \n");
              printf("3. DISPLAY \n");
              printf("4. EXIT \n");
              scanf("%d",&choice);
              switch(choice)
              {
                   case 1: //If user wishes to insert element in the queue
                   printf("Enter number to be inserted in the queue: ");
                   scanf("%d",&num); //getting the element to be inserted from the user
                   insert(num); //calling insert function
                   break; //jumping out of switch statement
                   case 2://If user wishes to delete element from the queue
                        delete(); //calling delete function and saving the result in an int variable(num)
                        break;
                        case 3://If user wishes to display the queue
                             display(); //calling display function
                             break;
                             case 4: //If user wishes to exit(stop continuing the program)
                                  exit(1); //end of while loop
                                  default ://If user enters a number other than 1 to 4
                                          printf("INVALID CHOICE \n");
                                          }
                                          }
                                          }

Also see Program of Operation on Queue as Array

OUTPUT: 


Thursday, September 29, 2016

Binary To Decimal Conversion

Program To Convert Binary Number To Decimal:


#include<iostream>
#include<math.h> //for power function
using namespace std;
main()
{
    int binary,decimal=0,i=0,remainder;
    cout<<"\nEnter The Binary Number ";
    cin>>binary;
    while(binary!=0)
    {
        remainder=binary%10; // getting the last digit
        binary=binary/10;
        decimal +=remainder*pow(2,i);//finding 2^i*digit
        i++;
    }
    cout<<"Decimal number is "<<decimal;
}

Also See Program To Perform Operations On Queue

Output:

Wednesday, September 28, 2016

Operations on QUEUE :)

Tags
#include<stdio.h>
#include<conio.h>
#define MAX 20 //defining size of queue
void insert(int); //function declaration for insertion
int del();        //function declaration for deletion
void display();   //function declaration for displaying queue
int queue[MAX];   //queue array
int front=-1,rear=-1;     /*initialy setting both parameters to -1 position i.e out of the array*/
main()
{
      int choice,num; //variables that we require in the entire program
      while(1) //for repeatingly asking the user whether to continue the program?
      {
              printf("Enter your choice\n");
              printf("1. INSERT \n");
              printf("2. DELETE \n");
              printf("3. DISPLAY \n");
              printf("4. EXIT \n");
              scanf("%d",&choice); //getting the choice of the user
              switch(choice) //using switch statement to select the working of the entered(desired) choice
              {
              case 1: //If user wishes to insert element in the queue
                   printf("Enter number to be inserted in the queue: ");
                   scanf("%d",&num); //getting the element to be inserted from the user
                   insert(num); //calling insert function
                   break; //jumping out of switch statement
                   case 2://If user wishes to delete element from the queue
                        num=delete(); //calling delete function and saving the result in an int variable(num)
                        break;
                        case 3://If user wishes to display the queue
                             display(); //calling display function
                             printf("\n");
                             break;
                             case 4: //If user wishes to exit(stop continuing the program)
                                  exit(1); //end of while loop
                                  default ://If user enters a number other than 1 to 4
                                          printf("INVALID CHOICE \n");
                                          }
                                          }
                                          }
                                          //FUNCTION DEFINATION
      void insert (int x) //value of number to be inserted(num) gets copied in 'x' when function is called
      {
           if(rear==MAX-1) //rear is at the last position in queue array
           {
                          printf("Queue is full (OVERFLOW)\n");
                          return; //returning control back to calling function
                          }
                          if (front==-1)//only if we wish to insert 1st element in the queue
                          {
                                       front=0;//front is set to 1st position i.e queue[0] and here the first insertion will take place
                                       }
                                       rear=rear+1; //insertion take place at REAR
                                       queue[rear]=x;//element to be inserted is placed at the rear position of queue
                                       }//for eg: for 1st insertion: front is made 0 and rear is also made 0(-1+1)
                                       //and so the insertion will take place at queue[0]
                                       //for next insertion rear will be 1(0+1) and front will remain 0 as insertion occurs at REAR
        int delete () //for deleting element from queue
        {
            int x;
            if(front==-1 || front==rear+1) //front==-1 means front is yet not incremented or set to 0 for first insertion
                     //front==rear+1 : if for eg front=0,rear=2
                     //for first deletion front=1,rear=2;for second deletion front=2,rear=2;now if front=3(rear+1),there will be no element in the queue
                        {              
                        printf("Queue is empty (UNDERFLOW) \n");
                        }
                        x=queue[front];//saving value of element at FRONT(to be deleted) in a variable
                        front=front+1;//deletion occurs at FRONT,and so we increment it for each deletion
                        printf("%d has been deleted \n",x);
                        return x;
                        }
       void display() //for displaying queue
       {
            int i;
            if(front==-1 || front==rear+1) //checking if queue is empty
            {
                        printf("Queue is EMPTY \n");
                        }
                        //if queue is not empty:
                        for(i=front;i<=rear;i++)//start printing elements of queue from FRONT to REAR
                        {
                                printf("%d ",queue[i]);//displaying QUEUE
                                }
                                }
                             
                                               
                     
                                         

Cube Root Of A Number

Tags

Program To Calculate Cube root of a Given Number:


// program to calculate cuberoot of a number
#include<iostream>
#include<math.h>
using namespace std;
main()
{
float num,cuberoot;
cout<<"\nEnter Any Number To Find Cube Root ";
cin>>num;
cuberoot=pow(num,1.0/3.0);
cout<<"\nCuberoot Of Given Number "<<cuberoot;
}

Also See Program To Convert Decimal To Hexadecimal Click Here
OUTPUT:


Decimal To Hexadecimal Conversion

Program To Convert Decimal Number To Hexadecimal Number:


#include<iostream>
using namespace std;
main()
{
int num,hexa[50],i=0;
cout<<"\nEnter Any Decimal Number ";
cin>>num;
while(num>0)
{
hexa[i]=num%16;
num=num/16;
i++;
}
cout<<"\nHexadecimal Equivalent is ";
for(int j=i-1;j>=0;j--)
{
switch(hexa[j])
{
case 10:
cout<<"A";
break;
case 11:
   cout<<"B";
break;
case 12:
   cout<<"C";
break;
case 13:
   cout<<"D";
break;
case 14:
   cout<<"E";
break;
case 15:
   cout<<"F";
break;
default:
cout<<hexa[j];
}}
}
Also See Program To Convert Decimal TO Octal Click Here

Output:


Decimal To Octal Conversion

Tags

Program To Convert Decimal Number To Octal Equivalent:


#include<iostream>
using namespace std;
main()
{
int num,octal[50],i=0;
cout<<"\nEnter Any Decimal Number ";
cin>>num;
while(num>0)
{
octal[i]=num%8;
num=num/8;
i++;
}
cout<<"\nOctal Equivalent is ";
for(int j=i-1;j>=0;j--)
cout<<octal[j];
}

Also see our program to convert Decimal To Binary Click Here
Output:


Decimal To Binary Conersion

Program to Convert Decimal Number To Binary Equivalent:


#include<iostream>
using namespace std;
main()
{
int num,binary[50],i=0;
cout<<"\nEnter Any Decimal Number ";
cin>>num;
while(num>0)
{
binary[i]=num%2;   //finding remainder
num=num/2;
i++;
}
cout<<"\nBinary Equivalent is ";
for(int j=i-1;j>=0;j--)  //from down to top moving
cout<<binary[j];
}
Also see program to evaluate postfix expression Click Here

Output:


Tuesday, September 27, 2016

Postfix Evaluation

Tags

Program To evaluate Postfix Expression:

#include<iostream>
#include<ctype.h>  //for isalnum function
using namespace std;
int stack[100];
int top=-1;
void push(int x)
{
stack[++top]=x;
}
int pop()
{

return (stack[top--]);
}
main()
{
char c,postfix[100];
int i=0,y,x;
cout<<"\nEnter The Postfix Expression:";
cin.getline(postfix,100);
while((c=postfix[i++])!='\0')
{
if(isdigit(c))
push(c-'0');  //for converting datatype
else
{
x=pop();
y=pop();
switch(c)
{
case'+':
push(x+y);
    break;
    case '-':
    push(y-x);
    break;
   case '*':
push(y*x);
break;
case '/':
push(y/x);
break;
}
}
}
cout<<"\n\nEntered Expression:"<<postfix;
cout<<"\n\nResult Of Evaluation:"<<stack[top];
}

PALLINDROME PRODUCTS:

Tags
This C++ Program Displays all the Products of two 2-digit numbers that are PALLINDROME.

#include <iostream>
using namespace std;
long int pal(long int);
int main()
{
    int i,j;
    long int num;
    cout<<"Pallindrome Products are:\n";

start:
    for(i=10; i<100; i++)
    {
        for(j=10; j<100; j++)
        {
            num=i*j;
            if(pal(num)!=0)
            {
                cout<<num<<" = "<<i<<" * "<<j;
                cout<<endl;
            }
        }
    }
    return 0;
}

long int pal(long int x)
{
    int rem,tmp,rev=0;
    tmp=x;
    while(tmp!=0)
    {
        rem=tmp%10;
        tmp/=10;
        rev=rev*10+rem;
    }
    if(x==rev)
        return x;
    else
        return 0;
}


MAX MOD 5:

Tags
Given two integer values, return whichever value is larger. However if the two values have the same remainder when divided by 5, then the return the smaller value. However, in all cases, if the two values are the same, return 0.

#include <iostream>
using namespace std;
int func(int,int);
int main()
{
int a,b;
cout<<"Enter First Number: ";
cin>>a;
cout<<"\nEnter Second Number: ";
cin>>b;
func(a,b);
return 0;
}
int func(int x,int y)
{
int rem1,rem2;
rem1=x%5;
rem2=y%5;
if(x==y)
return 0;

else
{
cout<<"\nAnswer is: ";
if(rem1==rem2)
x>y? cout<<y:cout<<x; //Conditional Operator

else
x>y? cout<<x:cout<<y;
}
}




Sunday, September 25, 2016

Static Members Both Data And Function

Tags

Program To Illustrate The Concept Of Static Members :


#include<iostream>
using namespace std;
class datatype
{
    int a;
    static int count;
    public:
        datatype()
        {
            a=++count;
        }
        void show()
        {
            cout<<"\nValue of Variable is "<<a;
        }
        static void showcount()
        {
            cout<<"\nNo of Objects cretaed "<<count;
        }
};
int datatype::count;

main()
{
    datatype a,b,c;
    a.show();
    b.show();
    c.show();
    datatype::showcount();
}

Number Of Words In a Text

Tags

Program to Calculate Number Of Words In a Text:


#include<iostream>
#include<string.h>
using namespace std;
main()
{
char string[100];
int l,ans=1;
    cout<<"Enter Text\n";
    cin.getline(string,100);
    l=strlen(string);
    cout<<"Total No of Words ";
    for(int i=0;i<l;i++)
    {
    if(string[i]==' ')
    {
    ans++;
}
}
    cout<<ans;
}
Special Thanks To Mr.Nikhil Aggarwal for this idea



TRIANGLE NUMBERS :)

Tags
This C++ Program Displays First 'n' Triangle Numbers.

The Sequence of Triangle Numbers are Generated by Adding Natural Numbers.
So the 5th Triangle Number Would be 1+2+3+4+5=15.

#include <iostream>
using namespace std;
int main()
{
int i,j,n,ans=1;
cout<<"Enter value of n: ";
cin>>n;
cout<<"\nFirst "<<n<<" Triangle Numbers are:\n";
cout<<"1\n";
for(i=2;i<=n;i++)
{
ans+=i;
cout<<ans<<"\n";
}
return 0;
}



PASCAL TRIANGLE:

Tags
C++ Program to Display Pascal Triangle.

#include <iostream>
using namespace std;
int main()
{
int a[20][20],i=0,j=0,n;
cout<<"Enter Size of Pascal Triangle: ";
cin>>n;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
a[i][j]=0;
}

a[0][0]=1;  //Making First and Second Rows as 1's
a[1][0]=1;
a[1][1]=1;

for(i=2;i<n;i++)
{
a[i][0]=1;
for(j=1;j<n;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
}

cout<<"\nPascal Triangle is:\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
cout<<a[i][j];
cout<<"\t";
}
cout<<endl;
}
return 0;
}


Transforming INFIX to POSTFIX Expression

Tags

Program to Convert An Infix Expression To Postfix Expression:


#include<stdio.h>
#include<ctype.h> //for isalnum function
char stack[50];
int top=-1;
void push(char x)
{
if(top==49)
{printf("Overflow");
}
else{

stack[++top]=x;
}
}
char pop()
{ if(top==-1)
{
return -1;
}
else{

return stack[top--];
}
}
int priority(char x)
{
if(x=='(')
return 0;
else if(x=='+'||x=='-')
 return 1;
 else if(x=='*'||x=='/')
 return 2;
}
main()
{
char infix[50];
char x,*e;  //pointer to access each character
printf("Enter The Expression  ");
scanf("%s",infix);
e=infix;
while(*e!='\0')
{
if(*e=='(')
{
push(*e);
}
else if(isalnum(*e))
{
printf("%c",*e);
}
else if(*e==')')
{
while((x=pop())!='(')
printf("%c",x);
}
else
{
while(priority(stack[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
e++;}
while(top!=-1)
printf("%c",pop());
}

Output:


Hybrid Inheritance

Hybrid Inheritance is that type in which we combine two or more types of inheritance.

Program To Illustrate the Concept of Hybrid Inheritance:

#include<iostream>
using namespace std;
class Student
{
int rollno;
public:
void getnum(int a)
{
rollno=a;
}
void putnum()
{
cout<<"Roll Number is "<<rollno;
}
};
class Marks:public Student
{ protected:
float sub1,sub2;
public:
void getmarks(float a,float b)
{
sub1=a;
sub2=b;
}
void display()
{
cout<<"\nSubject 1="<<sub1;
cout<<"\nSubject 2="<<sub2;
}

};
class sports
{protected:
int sport;
public:
void getsport(int a)
{
sport=a;
}
void displaysport()
{
cout<<"\nMarks In Sports="<<sport;
}
};
class result:public sports,public Marks
{
float total;
public:
void output()
{total=sub1+sub2+sport;
cout<<"\nTotal Marks="<<total;
}
};
main()
{
result r;
r.getnum(712299);
r.getmarks(98.5,89.6);
r.getsport(89);
r.putnum();
r.display();
r.displaysport();
r.output();
}

Also See Hierarchical Inheritance



Hierarchial Inheritance

Program to Illustrate The Concept Of Hierarchical Inheritance:


#include<iostream>
using namespace std;
class Number
{
int num;
public:
void getnum()
{
cout<<"\nEnter any Number ";
cin>>num;
}
int returnum()
{
return num;
}
};
class Square:public Number
{
int sqr;
public:
void square()
{
int num=returnum();
sqr=num*num;
cout<<"Square of "<<num<<" is "<<sqr;
}
};
class Cube:public Number
{
int cube;
public:
void cuube()
{
int num=returnum();
cube=num*num*num;
cout<<"Cube of "<<num<<" is "<<cube;
}
};
main()
{
Square a;
a.getnum();
a.square();
Cube b;
b.getnum();
b.cuube();
}
Also See Multiple Inheritance

Saturday, September 24, 2016

Multiple Inheritance

Program To Illustrate The Concept Of Multiple Inheritance:


#include<iostream>
using namespace std;
class base1
{
    int a;
    protected:
        int b;
    public:
        int c;
        void getdata()
        {
            cout<<"Enter The Values Of A,B,C\n";
            cin>>a>>b>>c;
        }
        void putdata()
        {
            cout<<"Values Are\n";
            cout<<"\nA="<<a;
            cout<<"\nB="<<b;
            cout<<"\nC="<<c;
                }
};
class base2
{
    int d;
    protected:
        int e;
    public:
        void input()
        {
            cout<<"Enter D and E\n";
            cin>>d>>e;
        }
        void output()
        {
            cout<<"\nD="<<d;
            cout<<"\nE="<<e;
        }
};
class derived:public base1,public base2
{
    int f;
    protected:
        int g;
    public:
        void getfg()
        {
            cout<<"Enter F and G\n";
            cin>>f>>g;
        }
        void putfg()
        {
            cout<<"\nF="<<f;
            cout<<"\nG="<<g;
        }
};
main()
{
    derived p;
    p.getdata();
    p.input();
    p.getfg();
    p.putdata();
    p.output();
    p.putfg();
}

Output:

Ambiguity:

In Multiple Inheritance There is an Ambiguity That If both Base Classes have same function then which function will be invoked by the object of derived class.
Because both functions are the members of derived class.
Therefore there will be an error message stating that the call is ambiguous.

Solution: 

We can solve the above problem by using scope resolution operator.

p.base1::putdata();

p.base2::putdata();

Error Message:







Multilevel Inheritance

Multilevel:

It is a type of inheritance in which the derived class is used as base class to derive another class.
Thus the second derived class has features of both base and 1 st derived class.

 Program to Illustrate The Multilevel Inheritance:


#include<iostream>
using namespace std;
class base
{
    int a;
    protected:
        int b;
    public:
        int c;
        void getdata();
        void display();
};
void base:: getdata()
{
    cout<<"Enter The Values Of A,B,C\n";
    cin>>a>>b>>c;
}
void base::display()
{
    cout<<"Values Are\n";
    cout<<"A="<<a;
    cout<<"\nB="<<b;
    cout<<"\nC="<<c;
}
class derive:public base
{
    int d;
    protected:
        int e;
    public:
    void displaay()
    {
        cout<<"\nD="<<d;
        cout<<"\nE="<<e;
    }
            void input()
            {
                cout<<"\nEnter D AND E\n";
                cin>>d>>e;
       
            }
};
class derive1:public derive
{
    int f;
    protected:
        int g;
    public:
        void getfg()
        { cout<<"Enter F And G\n";
            cin>>f>>g;
        }
        void putfg()
        {
        cout<<"\nF="<<f;
        cout<<"\nG="<<g;
        }
};

main()
{
    derive1 p;
    p.getdata();
    p.input();
    p.getfg();
    p.display();
    p.displaay();
    p.putfg();
}