Tuesday, 31 December 2013

C++ program to differentiate Local variable

#include<iostream.h>
void function()
{
  int j=10;
  cout<<j<<"\n" ;
  j++;
}
void main()
{
       clrscr();
     int i=10;
     for(i=0;i<10;i++)
     {
       function();
     }
}

Global Variable Example Using C++

#include<iostream.h>
int count;                               /* count is global*/
void function1();
void main()
{
 clrscr();

  count=100;

   function1();

   getch();

}
void function1()
{
   int temp;
   temp=count;
   cout<<"\n the value of temp and count ::" <<temp<<" " <<count;

}

C++ Program To Print The Perfect Square Number Which is Less Than 1000

#include<iostream.h>
void main()
{
   clrscr();
   int n=1000, s=0;
   cout<<"The Squre values Which is less Than 1000 is::  " ;

   for(int i=1;s<n;i++)
    {
      s=i*i;
      if(s>n)
      break;

     cout<<s<<" ";

    }


       getch();

}

C++ program to finding the maximum element in an array

#include<conio.h>
#include<iostream.h>
#include<stdio.h>
void main()
{
      clrscr();

    int array[]={1,2,3,4,5};

  int var1=array[0];
  for(int i=1;i<=5;i++)
   {  if(var1<array[i])
       var1=array[i];
   }
  cout<<var1;
       getch();

}

Saturday, 28 December 2013

C Progran to Implement 8 Queen's Problem using Backtracking

#include<stdio.h>
#include<conio.h>
#include<math.h>

char a[10][10];
int n=8;

void printmatrix()
{
 int i,j;
 printf("n");
 for(i=0;i < n;i++)
       {
       for(j=0;j < n;j++)
            printf("%ct",a[i][j]);
       printf("nn");
       }
}

//------------------------------------------

int getmarkedcol(int row)
{
 int i,j;
 for(i=0;i < n;i++)
     if(a[row][i]=='Q')
     {
     return(i);
     break;
     }
}

//------------------------------------------

int feasible(int row, int col)
{
 int i,tcol;
 for(i=0;i < n;i++)
     {
     tcol=getmarkedcol(i);
         if(col==tcol || abs(row-i)==abs(col-tcol))
              return 0;
     }
 return 1;
}

//------------------------------------------

void nqueen(int row)
{
 int i,j;
 if(row < n)
 {
  for(i=0;i < n;i++)
     {
     if(feasible(row,i))
         {
         a[row][i]='Q';
         nqueen(row+1);
         a[row][i]='.';
         }
     }
 }
 else
 {
 printf("nThe solution is:- ");
 printmatrix();
 }
}

//---------------------------------

void main()
{
 int i,j;
 clrscr();

 printf("8 Queen's Problem using Backtracking ");


 for(i=0;i < n;i++)
     for(j=0;j < n;j++)
  a[i][j]='.';

 nqueen(0);
 getch();
}

C++ Program having functions setmarks inside which 3 subject marks should be set & Make total a friend function inside which all 3 subject marks total will be done

#include<iostream.h>
  class Marks
{
  int a;
  int b;
  int c;
  public:
  void setmarks()
  {
  cout<<"Enter Marks Of Subject ";
  cin>>a>>b>>c;
  }
friend int total(Marks m);
};
int total(Marks m)
{
  return(m.a+m.b+m.c);
}
Void main()
{
  Marks x;
  x.setmarks();
  cout<<total(x);
}

C++ Program to find area of square and rectangle using function overloading


#include<iostream.h>
int area(int);
int area(int ,int );
void main()
{
  cout<<area(10);
  cout<<area(5,2);
}
int  area (int  l)
{
  return(l*l);
}
int area (int l,int b)
{
  return(l*b);
}