Friday 8 April 2016

WAP to understand destructure


#include<iostream.h>
#include<conio.h>
class con
  {
  int f0,f1,fbi;
  public:
con()
{
f0=0;
f1=1;
fbi=f0+f1;
}
~con()
{
cout<<"Data is deleted from memory";
}
void inc()
{
f0=f1;
f1=fbi;
fbi=f0+f1;
}
void dis()
{
cout<<fbi<<endl;
}
};
   void main()
   {
   con obj;
   int i;
   for(i=0;i<=5;i++)
   {
   obj.dis();
   obj.inc();
   getch();
   }
   }

WAP for destructor

#include<iostream.h>
#include<conio.h>
class Line{
  public:
void setlength(double len);
double getlength(void);
 Line();
~Line();

  private:
 double length;
};
Line::Line(void)
{
  cout<<"
Object is being crreted";
}
Line::~Line(void)
{
  cout<<"
Destructor is called";
}

void Line::setlength(double len)
{
  length=len;
}
double Line::getlength(void)
{
   return length;
}
void main(){
 Line l1;
 clrscr();
 l1.setlength(3.44);
 cout<<"
 Length of line : "<<l1.getlength();
 getch();
}

WAP for constructor overloading

#include<iostream.h>
#include<conio.h>
#include<string.h>
class emp
{
private:
char name[20];
int age;
float bs;
public:
void read()
{
cout<<"name,age,bs
";
cin>>age>>bs;
}
void write()
{
cout<<"name="<<name<<"age="<<age<<"basic salary="<<bs;
}
emp()
{
strcpy(name," ");
age=0;
bs=0;
}
emp(char *n,int a,float b)
{
strcpy(name,n);
age=a;
bs=b;
}
/*emp(char *n,int a=0,float b=0)
{
strcpy(name,n);
age=a;
bs=b;
} */
emp(emp &e)
{
strcpy(name,e.name);
age=e.age;
bs=e.bs;
}
};
void main()
{
emp e1,e2("Ashish",25,29000.99),e3(e2),e4("Gadpayle",22,7000.99);
clrscr();
e1.write();
e2.write();
e3.write();
e4.write();
getch();
}

WAP for constructor with arguments in base

# include <iostream.h>
# include <conio.h>
class base
{
  public:
  base(int x)
  {
    cout<<"X VALUE IS  "<<x<<endl;
    cout<<"INSIDE BASE CLASS CONSTRUCTOR "<<endl;
  }
  void display()
  {
    cout<<"INSIDE DISPLAY OF BASE"<<endl;
  }
};
class derived : public base
{
  public:
  derived(int x,int y) : base(x)
  {
    cout<<"Y VALUE IS   "<<y<<endl;
    cout<<"INSIDE DERIVED CLASS CONSTRUCTOR  "<<endl;
  }
  void print()
  {
    cout<<"INSIDE PRINT OF DERIVED"<<endl;
  }
};
void main()
{
  clrscr();
  derived d(10,20);
  d.display();
  d.print();
  getch();
}

WAP to understand the constructor

#include<iostream.h>
#include<conio.h>
class code
{
 public:
int id;
code()
{
id=0;
}
code (int a)
{
id=a;
}
code (code &x)
{
id=x.id;
}
void display()
{
cout<<"
 id="<<id;
}
};
void main()
{
       // int a;
       // cout<<"
 enter number :";
       // cin>>a;
code c;
c.display();
code d(50);
d.display();
code c1);
c1.display();
getch();
clrscr();
}

WAP for area using default constructor

#include<iostream.h>
#include<conio.h>
class Area{
  int len,bre;
  public:
Area(){
 len=5;
 bre=6;
}
void getlength(){
 cout<<"
Enter length and breadth ";
 cin>>len>>bre;
}

int areacal()
{
 return(len*bre);
}
void display(int temp)
{
 cout<<"
Area = "<<temp;
}
};


void main(){
  Area a1,a2;
  clrscr();
  int temp;
  cout<<"
For Default ";
  temp=a1.areacal();
  a1.display(temp);
  cout<<"
For Paramerised ";
  a2.getlength();
  temp=a2.areacal();
  a2.display(temp);
  getch();
}

WAP for default & parameterised constructor

#include<iostream.h>
#include<conio.h>
class Line{
  public:
Line(double len);
void setlength(double len);
double getlength(void);
  private:
 double length;
};
Line::Line(double len)
{
  length=len;
}
void Line::setlength(double len)
{
  length=len;
}

double Line::getlength(void)
{
   return length;
}
void main(){
 Line l1(6.3);
 clrscr();
 cout<<"
 Default : "<<l1.getlength();
 l1.setlength(3.44);
 cout<<"
 Paramerised : "<<l1.getlength();
 getch();
}

WAP for game constructo

#include<iostream.h>
#include<conio.h>
class Game{
  int goals;
  public:
Game(){
  goals=0;
}
int getgoals(){
return goals;
}

int incrementgoals()
{
 return goals++;
}
};


void main(){
  Game g;
  clrscr();
  cout<<"
No od goal when game is stared..       : "<<g.getgoals();
  g.incrementgoals();
  g.incrementgoals();
  cout<<"
No od goal when game is little later.. : "<<g.getgoals();
  getch();
  }

WAP for copy constructor

#include<iostream.h>
#include<conio.h>
class Game{
  int goals;
  public:
Game(){
  goals=0;
}
int getgoals(){
return goals;
}

int incrementgoals()
{
 return goals++;
}
};


void main(){
  Game g;
  clrscr();
  cout<<"
No od goal when game is stared..       : "<<g.getgoals();
  g.incrementgoals();
  g.incrementgoals();
  cout<<"
No od goal when game is little later.. : "<<g.getgoals();
  getch();
  }

WAP for bubble sort

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
 int a[20],i,j,n,temp;
 cout<<"\n Enter size of array :";
 cin>>n;
 cout<<"\n Enter element of array  ";
 for(i=1;i<=n;i++)
 cin>>a[i];
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
  if(a[j]>a[j+1])
  {
  temp=a[j];
  a[j]=a[j+1];
  a[j+1]=temp;
  }
}
}
 cout<<"\n The sorted array are :";
 for(i=1;i<=n;i++)
 cout<<"    "<<a[i];
 getch();
 clrscr();
}
/* output

  
 Enter size of array    :6

 Enter element of array  12 45 32 14 10 26

 The sorted array are   :    10    12    14    26    32    45

WAP for binary search method

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
 int a[15],i,n,lb,ub,mid,item;
 cout<<"\n Enter size of array :";
 cin>>n;
 cout<<"\n Enter sorted element for array  :";
 for(i=1;i<=n;i++)
 cin>>a[i];
 cout<<"\n Enter item which is to be search  :";
 cin>>item;
   lb=1;
   ub=n;
   mid=int((lb+ub)/2);
   while(a[mid]!=item && lb<=ub)
   {
    if(item<a[mid])
     ub=mid-1;
    else
     lb=mid+1;
    mid=int((lb+ub)/2);
   }
  if(a[mid]==item)
  {
  cout<<"\n search is successful ";
  cout<<"\n location is= "<<mid;
  }
  else
     cout<<"\n Search is unsuccessful ";
 getch();
 clrscr();
}

/* output

  
 Enter size of array    :5

 Enter sorted element for array  :11 22 33 44 55

 Enter item which is to be search  :44

 search is successful
 location is= 4   */

program to find largest two number

#include<iostream.h>
#include<conio.h>
void main()
{
 int a[22],fbig=0,sbig=0,i,n;
 cout<<"\n enter size :";
 cin>>n;
 cout<<"\n enter element :";
 for(i=1;i<=n;i++)
 cin>>a[i];
 fbig=a[1];
 for(i=2;i<=n;i++)
 {
if(fbig<a[i])
{
fbig=a[i];
}
 }
 sbig=a[2];
 for(i=3;i<=n-1;i++)
 {
if(sbig<a[i])
{
sbig=a[i];
}
 }

 cout<<"\nfirst largest :"<<fbig;
 cout<<"\n second largest :"<<sbig;
 getch();
 clrscr();
}

WAP for sum of elements in matrix

#include<iostream.h>
#include<conio.h>
void main()
{
 int a[5][5],n,m,i,j,small;
 int sum=0;
 cout<<"\n enter size of rows :";
 cin>>n;
 cout<<"\n enter size of collumns  :";
 cin>>m;
 cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
 cout<<"\n 2d array \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
sum=sum+a[i][j];
}
cout<<"\n";
}
cout<<"\n The sum of every element :"<<sum;
 getch();
 clrscr();
 }


WAP to find small no in matrix

#include<iostream.h>
#include<conio.h>
void main()
{
 int a[5][5],n,m,i,j,small;
 cout<<"\n enter size of rows :";
 cin>>n;
 cout<<"\n enter size of collumns  :";
 cin>>m;
 cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
 cout<<"\n 2d array \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}
 small=a[1][1];
 for(i=2;i<=n;i++)
 {
for(j=1;j<=m;j++)
{
if(small>a[i][j])
{
small=a[i][j];
}
}

 }
 cout<<"\n The smallest element is "<<small;
 getch();
 clrscr();
}

WAP for linear search using while

#include<conio.h>
#include<iostream.h>
void main()
{
 int item,a[20],n,i,loc;
 cout<<"\n enter size of array :";
 cin>>n;
 cout<<"\n enter element of array :";
for(i=1;i<=n;i++)
cin>>a[i];
 cout<<"\n enter element which is to be search :";
 cin>>item;
a[n+1]=item;
loc=0;
while(a[loc]!=item)
{
loc++;
}
if(loc==n+1)
{
loc=0;
cout<<"\n ITEM is not found :";
}
else
cout<<"\n ITEM is found at location :"<<loc;
 getch();
 clrscr();
}
/* OUTPUT

  
 enter size of array    :6

 enter element of array :12 45 23 52 48 45

 enter element which is to be search    :23

 ITEM is found at location      :3

WAP for finding a location

#include<iostream.h>
#include<conio.h>
void main()
{
 int a[4][4],n,m,i,j,loc=0,key;
 cout<<"\n enter size of rows :";
 cin>>n;
 cout<<"\n enter size of collumns  :";
 cin>>m;
 cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
 cout<<"\n 2d array \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}
cout<<"\n\n Enter element which location is to be search  :";
cin>>key;
for(i=1;i<=n;i++)
{
 for(j=1;j<=m;j++)
 {
   if(a[i][j]==key)
   {
    cout<<"\nelemt is present at "<< i << "row and "<<j<< "column";
    break;
   }
 }
}
getch();
clrscr();
 }

WAP to sort elements in ascending order

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
 int a[10],i,j,n,temp,k,ptr;
 cout<<"\n Enter size of array :";
 cin>>n;
 cout<<"\n Enter element of array :";
 for(i=1;i<=n;i++)
 cin>>a[i];
for(i=2;i<=n;i++)
{
temp=a[i];
ptr=i-1;
 while(temp<a[ptr])
 {
  a[ptr+1]=a[ptr];
  ptr--;
 }
  a[ptr+1]=temp;
}
 cout<<"\n The sorted elements are :\n";
 for(i=1;i<=n;i++)
 cout<<"    "<<a[i];
 getch();
 clrscr();
}
/* OUTPUT

  
 Enter size of array    :7

 Enter element of array :15 23 10 22 48 75 12

 The sorted elements are        :
    10    12    15    22    23    48    75

WAP to insert element into array

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
 int i,loc,n,new1,a[20];
 cout<<"\n enter size of array :";
 cin>>n;
 cout<<"\n enter element of array :";
for(i=1;i<=n;i++)
cin>>a[i];
 cout<<"\n enter element is to be insert :";
 cin>>new1;
 cout<<"\n enter location where element is insert :";
 cin>>loc;
i=n;
while(i>=loc)
{
a[i+1]=a[i];
i--;
}
 a[loc]=new1;
 n++;
 cout<<"\n The result is :";
for(i=1;i<=n;i++)
cout<<"    "<<a[i];
 getch();
 clrscr();
}

/* output

  
 enter size of array    :5

 enter element of array :11 62 45 78 12

 enter element is to be insert :33

 enter location where element is insert :3

 The result is  :    11    62    33    45    78    12

WAP to count the repeated location

#include<conio.h>
#include<iostream.h>
void main()
{
 int item,a[20],n,i,count=0,loc=0;
 cout<<"\n enter size of array :";
 cin>>n;
 cout<<"\n enter element of array :";
for(i=1;i<=n;i++)
cin>>a[i];
 cout<<"\n enter element which is to be search :";
 cin>>item;
 cout<<"\n The no of location is   :";
i=1;
while(i<=n)
{
if(a[i]==item)
{
count=count+1;
cout<<i<<",\t";
i++;
}
else
{
i++;
}
}
 if(a[i]==item)
 cout<<"\n Total no. of repeated given element :"<<count;
 else
 cout<<"\n element is found";
 getch();
 clrscr();
}

WAP for copy one array into anather array

#include<conio.h>
#include<iostream.h>
void main()
{
 int a[20],b[20],n,i;
 cout<<"\n enter size of array :";
 cin>>n;
 cout<<"\n enter element of array :";
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n;i++)
{
b[i]=a[i];
}
 cout<<"\n The copy of array is :";
 for(i=1;i<=n;i++)
 cout<<b[i];
 getch();
 clrscr();
}

WAP for addition of matrix.

#include<iostream.h>
#include<conio.h>
void main()
{
 int a[4][4],b[4][4],c[4][4],n,m,i,j;
 cout<<"\n enter size of rows :";
 cin>>n;
 cout<<"\n enter size of collumns  :";
 cin>>m;
 cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
 cout<<"\n first matrix \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n\n";
}
 cout<<"\n enter same size of rows :";
 cin>>n;
 cout<<"\n enter same size of collumns  :";
 cin>>m;
 cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>b[i][j];
}
}
 cout<<"\n second matrix \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<"\n\n";
}
for(i=1;i<=n;i++)
{
 for(j=1;j<=m;j++)
 {
  c[i][j]=a[i][j]+b[i][j];
 }
}
 cout<<"\n addition of matrix is \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n\n";
}
 getch();
 clrscr();
 }

WAP for multiplication of matrix


#include<iostream.h>
#include<conio.h>
void main()
{
 int a[2][2],b[2][2],c[2][2],n,m,i,j,k;
 cout<<"\n enter size of rows :";
 cin>>n;
 cout<<"\n enter size of collumns  :";
 cin>>m;
 cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
 cout<<"\n first matrix \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n\n";
}
 cout<<"\n enter same size of rows :";
 cin>>n;
 cout<<"\n enter same size of collumns  :";
 cin>>m;
 cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>b[i][j];
}
}
 cout<<"\n second matrix \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<"\n\n";
}
for(i=1;i<=n;i++)
{
 for(j=1;j<=m;j++)
 {
  c[i][j]=0;
  for(k=1;k<=n;k++)
  {
  c[i][j]=c[i][j]+a[i][k]*b[k][j];
  }
 }
}
 cout<<"\n multiplication of matrix is \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n\n";
}
 getch();
 clrscr();
 }

Saturday 12 March 2016

WAP for working of static variable


#include<stdio.h>  
#include<conio.h>
void Check();
int main(){
   Check();
   Check();
   Check();
getch();
}
void Check(){
    static int c=0;
    printf("%d\t",c);
    c+=5;
}

WAP to use a simple function



#include<stdio.h>
#include<conio.h>
int fun(void);
main()
{
 int x=5;
 clrscr();
 printf("\n x = %d ",x);
 fun();
 getch();

}
int fun(void)
{
 int x=6;
 printf("\n x = %d ",x);

}

Tuesday 8 March 2016



WAP for simple calculator using switch case


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace switch_case
{
    class Program
    {
        static void Main(string[] args)
        {
          int  a, b, ch, add, div, mul, sub;
            Console.Write("Enter the 1st no=");
            a = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the 2nd no=");
            b = Convert.ToInt32(Console.ReadLine());
      
            Console.WriteLine("1.Addition");
            Console.WriteLine("2.Subtraction");
            Console.WriteLine("3.Multiplication");
            Console.WriteLine("4.Division");
                Console.Write("Enter your choice");
                ch = Convert.ToInt32(Console.ReadLine());
                switch (ch)
                {
                    case 1:
                        add = a + b;
                        Console.WriteLine(add);
                        break;
                    case 2:
                        sub = a - b;
                        Console.WriteLine(sub);
                        break;
                    case 3:
                        mul = a * b;
                        Console.WriteLine(mul);
                        break;
                    case 4:
                        div = a / b;
                        Console.WriteLine(div);
                    break;
                    default :
                        Console.WriteLine("wrong choice");
                        break;
                }
                Console.ReadLine();
        }
    }
}


OUTPUT:-



 

WAP for GCD of two numbers.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace vaishu
{
    class Program
    {
        static int gcd(int a, int b)
        {
            int temp;
            if (b == 0)
                return a;
            else
                while (b != 0)
                {
                    temp = b;
                    b = a % b;
                    a = temp;
                }
            return a;
        }



        static void Main(string[] args)
        {
            int n1, n2, gcd1;
            Console.WriteLine("Enter first number" );
            n1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter second number");
            n2 = Convert.ToInt32(Console.ReadLine());
            gcd1 = gcd(n1, n2);
            Console.WriteLine("gcd= " + gcd1);
            Console.ReadLine();
        }
    }
}




Output:
    
     



WAP for fibonacci series

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace fib
{
    class Program
    {
     
        static void Main(string[] args)
        {
            int f1 = 0, f2 = 1, f3 = 0;
            int n,i;
            Console.Write("Enter the limit= ");
            n = int.Parse(Console.ReadLine());

            Console.WriteLine(f1);
            Console.WriteLine(f2);
            for (i =3; i <= n; i++)
            {
                f3 = f1 + f2;
                f1 = f2;
                f2 = f3;
              
                Console.WriteLine(f3);

            }

 
                Console.ReadLine();
             
        }
    }
}



Output:

 

WAP for number is armstrong


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace arm
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum, temp, no, rem;
            Console.WriteLine("Armstrong no between 1 to 1000=");
            for (no = 1; no <= 1000; no++)
            {
                temp = no;
                sum = 0;

                while (temp > 0)
                {
                    rem = temp % 10;
                    sum = sum + (rem * rem * rem);
                    temp = temp / 10;
                }
                if (no == sum)
                {
                    Console.WriteLine("   " + no);
                  
                }

            }
            Console.ReadLine();
        }
    }
}



Output:

 


WAP to swap without using third variable.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace temporary
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b;
            Console.WriteLine("Before Swapping:- ");
            Console.Write("\nEnter the value of a= ");
            a=Convert.ToInt32(Console.ReadLine());
            Console.Write("\nEnter the value of b= ");
            b=Convert.ToInt32(Console.ReadLine());
            a = a + b;
            b = a - b;
            a = a - b;


            Console.WriteLine(" \n After Swapping:-\n");
            Console.WriteLine("\nvalue of a= "+a);
            Console.WriteLine("\nvalue of b= " + b);
            Console.ReadLine();

        }
    }
}

OUTPUT:-


WAP to access and mutate named properties.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class Example
    {
        int _number;
        public int Number
       {
            get
            {
                 return this._number;
             }
            set
            {
                this._number=value;
            }
        }
    }
     class Program
    {
        static void Main(string[] args)
        {
            Example example=new Example();
            example.Number=5;//set()
            Console.WriteLine("number="+example.Number);
            Console.ReadLine();
        }

    }
}

OUTPUT:-