Wednesday, 18 December 2013

C Program to implement 4-Queens Problem using Backtracking

#include<stdio.h>
#include<conio.h>
#include<math.h>
char a[10][10];
int n;
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("n Enter the no. of queens:- ");
       scanf("%d",&n);

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

}

Armstrong Number Using C++

/*Armstrong Number  Using C++ Program  CHECK THE GIVEN NUMBER IS ARMSTRONG OR NOT

FOR EXAMPLE
153/1^3+5^3+3^3=153
*/
#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int i,n,m,re,sum=0;
  cout<<"enter the required number";
  cin>>n;
  m=n;
  while(m>0)
   {
    re=m%10;
    m=m/10;
    sum=sum+re*re*re;
   }
  if(sum==n)
   {
    cout<<"given number"<<n<<"is armstrong";
   }
  else
   {
    cout<<"given number"<<<n<"is not armstrong";
   }
  getch();
}

Even or Odd For 1 to 100 Number Using C++


#include<iostream.h>
#include<conio.h>
void main()
{
  int  i;
  for(i=1;i<=100;i++)
  {
  if(i%2==0)
  {
                  cout<<i<<"  number is even \n “;
   }
  else
  {
                 cout<<i<<"   number is odd\n“;
  }
}
  getch();

}

Given Number Even Or Odd Using C++


#include<iostream.h>
#include<conio.h>
void main()
{
  int  num;
cout<<" Even or odd Number given by user\n “;
 cout<<" Enter number for verification  “;
 cin>>num;
if(num%2==0)
  {
  cout<<" Given number is even   “;
   }
  else
  {
      cout<<" Given number is odd“;
  }
 getch();
}

Fibonacci Series Using C++

/*Fibonacci  Series up to user Given number Using C++
   Series Limit is Defined by User
*/
#include<iostream.h>
#include<conio.h>
void main()
{
  int  n,i,fst=0,sec=1,num;
  cout<<" Enter number for Fibonacci  Series “;
  cin>>num;
  cout<<" Fibonacci Series 0 to” <<num <<”number”;
  for(i=0;i<= num;i++)
  {
  if(i<=1)
  {
    n=i;
  }
  else
  {
    n= fst+sec;
   fst=sec;
   sec=n;
  }
 Cout<<”\t”<<n;
 }
 getch();
}

PROGRAM TO ADDITION TWO NUMBERS USING C++



#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int a,b,sum;
  cout<<"enter  the value of number 1 and number 2";
  cin>>a>>b;
  sum=a+b;
  cout<<"\n"<<"sum of "<<" number 1 ="<<a<<" number  2="<<b<<”is”<<sum;
  getch();
}

Fibonacci Series up to 1 to 100 Using c++


#include<iostream.h>
#include<conio.h>
void main()
{
  int  n,i,fst=0,sec=1;
  cout<<" Fibonacci  Series up to 1 to 100";
  for(i=0;i<=100;i++)
  {
  if(i<=1)
  {
    n=i;
  }
  else
  {
    n= fst+sec;
   fst=sec;
   sec=n;
  }
Cout<<”\t”<<n;
}
  getch();
}