SQL Random Line

Function SQLRandomLines(proc, params, fname, count)
Dim i, j, k, rid, rs, result, records, reccount, normalized

On Error Resume Next

PushError

SQLRandomLines = Null

Set rs = iOpen(proc, params)
If CheckPopError Then
Exit Function
End If

If rs.EOF Then
rs.Close
Set rs = Nothing
Exit Function
End If

records = rs.GetRows(adGetRowsRest)
If CheckPopError Then
Exit Function
End If

rid = -1
For i = 0 To rs.Fields.Count - 1
If (rs.Fields(i).Name = fname) Then
rid = i
End If
Next
If (rid < 0) Or (rid > rs.Fields.Count - 1) Then
rid = -1
End If
rs.Close
Set rs = Nothing

If (rid = -1) Then
Exit Function
End If
reccount = UBound(records, 2) - LBound(records, 2) + 1

If (reccount >= count) Then
normalized = RandomArray(count, LBound(records, 2), UBound(records, 2))
For i = LBound(normalized) To UBound(normalized)
normalized(i) = records(rid, normalized(i))
Next
Else
normalized = RandomArray(reccount, LBound(records, 2), UBound(records, 2))
For i = LBound(normalized) To UBound(normalized)
normalized(i) = records(rid, normalized(i))
Next
End If

ReDim result(UBound(records, 1), UBound(normalized))
For k = LBound(records, 2) To UBound(records, 2)
For i = LBound(normalized) To UBound(normalized)
If (CStr(records(rid, k)) = CStr(normalized(i))) Then
For j = LBound(records, 1) To UBound(records, 1)
If (TypeName(records(j, k)) = "String") Then
result(j, i) = VarTrimStr(records(j, k))
Else
result(j, i) = records(j, k)
End If
Next
End If
Next
Next
SQLRandomLines = result
End Function

SQL Random ID

Function SQLRandomIDs(proc, params, fname, count)
Dim i, rs, records, reccount, normalized

On Error Resume Next

PushError

SQLRandomIDs = Null

Set rs = iOpen(proc, params)
If CheckPopError Then
Exit Function
End If

If rs.EOF Then
rs.Close
Set rs = Nothing
Exit Function
End If

records = rs.GetRows(adGetRowsRest, , fname)
If CheckPopError Then
Exit Function
End If
rs.Close
Set rs = Nothing

reccount = UBound(records, 2) - LBound(records, 2) + 1
If IsNull(count) Or IsEmpty(count) Then
count = reccount
ElseIf (count < 0) Then
count = reccount
End If

If (reccount >= count) Then
normalized = RandomArray(count, LBound(records, 2), UBound(records, 2))
For i = LBound(normalized) To UBound(normalized)
normalized(i) = records(0, normalized(i))
Next
Else
normalized = RandomArray(reccount, LBound(records, 2), UBound(records, 2))
For i = LBound(normalized) To UBound(normalized)
normalized(i) = records(0, normalized(i))
Next
End If
SQLRandomIDs = normalized
End Function

Remove Duplicate Records



Remove duplicate database records
Database:
.mdb
Table Name:
Field Name:
Primary Key:


Import the SQL Server error log into a table

CREATE PROC sp_import_errorlog
(
    @log_name sysname,
    @log_number Int = 0,
    @overwrite bit = 0
)
As

Purpose:    To import the SQL Server Error Log into a table, so that it can be queried

Tested On:  SQL Server 2000

Limitation:     With Error messages spanning more than one line only the first line Is included In the table

Example 1:  To import the current Error Log To table myerrorlog
        EXEC sp_import_errorlog 'myerrorlog'

Example 2:  To import the current Error Log To table myerrorlog, And overwrite the table
        'myerrorlog' if it already exists
        EXEC sp_import_errorlog 'myerrorlog', @overwrite = 1

Example 3:  To import the previous Error Log To table myerrorlog
        EXEC sp_import_errorlog 'myerrorlog', 1

Example 4:  To import the Second previous Error Log To table myerrorlog
        EXEC sp_import_errorlog 'myerrorlog', 2


BEGIN
    Set NOCOUNT On
  
    Declare @sql varchar(500) --Holds To SQL needed To create columns from Error Log

    If (Select OBJECT_ID(@log_name,'U')) IS NOT NULL
        BEGIN
            If @overwrite = 0
                BEGIN
                    RAISERROR('Table already exists. Specify another name or pass 1 to @overwrite parameter',18,1)
                    RETURN -1
                End
            Else
                BEGIN
                    EXEC('DROP TABLE ' + @log_name)
                End
        End

  
    --Temp table To hold the output of sp_readerrorlog
    CREATE TABLE #errlog
    (
        err varchar(1000),
        controw tinyint
    )

    --Populating the temp table using sp_readerrorlog
    INSERT #errlog
    EXEC sp_readerrorlog @log_number

    --This will remove the header from the errolog
    Set ROWCOUNT 4
    DELETE #errlog
    Set ROWCOUNT 0

  
    Set @sql =  'SELECT
                CONVERT(DATETIME,Left(err,23)) [Date],
                SUBSTRING(err,24,10) [spid],
                Right(err,Len(err) - 33) [Message],
                controw
            INTO ' + QUOTENAME(@log_name) +
            ' FROM #errlog ' +
            'WHERE controw = 0'
  
    --Creates the table With the columns Date, spid, message And controw
    EXEC (@sql)
  
    --Dropping the temporary table
    DROP TABLE #errlog
  
    Set NOCOUNT OFF
Print 'Error log successfully imported to table: ' + @log_name
End

Call an Oracle Stored Procedure

Assume you have a procedure like this one below, And that it has been already created On the
Oracle database. This procedure doesn't return anything, but that doesn't change anything!
STEP #1:
/******STORED PROCEDURE On ORACLE DATABASE************/
create Or Replace procedure test_me
Is
w_count integer;
begin
insert into TEST values ('Surya was here');
--commit it
commit;
end;
/*****End OF STORED PROCEDURE****/


STEP # 2:
+++++++++
I assume you have tested it from sql*plus by running the
following statements:

/************TEST THE STORED PROCEDURE FROM SQL*PLUS******/
SQL> execute test_me

PL/SQL procedure successfully completed.

SQL>
/***************End OF TESTING THE STORED PROC************/

STEP# 3:
++++++++
/*****CALLING A STORED PROCEDURE FROM ASP******************/

1. USING THE CONNECTION OBJECT

You can execute stored procedures which perform Oracle Server side tasks And return you a recordset. You can only use this method If
your stored procedure doesn't return any OUTPUT values.

Note that -1 means no count of total number of records Is
required. If you want To Get the count, substitute count
With some Integer variable

Note that 4 means it Is a stored procedure. By using the
actual number -1 And 4, you don't need the server side
include ADOVBS.INC ;-)

The above would Do the job On the database And return
back To you without returning any recordsets.

Alternatively, you could:



W_count Is the number of records affected. If your stored
procedure were To return a query result, it Is returned
within your recordset (rs). This method Is useful With Stored procs
which return results of an SQL query


2. USING THE COMMAND OBJECT



STEP# 4
+++++++++
/************PASSING Input/OUTPUT PARAMETERS**************************/

Add New Record with ADO



Author ID:


Author Name:


Year Born:







The form responder looks like this:















Here Is the include file that displays appropriate errors:

ADO Schemas to list tables & fields







Here Is the contents of lib_fieldtypes.asp which Is included To make this example work:



Database Paging




          
   


  
  
  
  
  
  
  
  
  
  



  

5K Race



Records: - of









  

  

  

  

  

  


  
NameAgeCityStateTimePace




      

  


How to filter a recordset

>Before Filter






























>After Filter


Index Server Access via ADO


Choose The Word You Want To Search For::

Search Word:





The iskeywordrespond.asp looks like this:













It has To exclude many folders On my site And the following file excludes directories:



Insert TEXT blob using ADO



















Using a Stored Procedure with ADO

Dim cn As New ADODB.Connection
cn.Open sConStr
Dim cmd As New ADODB.Command
cn.CursorLocation = adUseClient
Set cmd.ActiveConnection = cn
cmd.CommandText = "addNew_Service"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Refresh
cmd.Parameters.Item("@wsdlfilename") = CStr(services(serviceId).wsdlfilename)
cmd.Parameters.Item("@WSMLFileName") = CStr(services(serviceId).WSMLFileName)
cmd.Parameters.Item("@name") = CStr(services(serviceId).name)
cmd.Parameters.Item("@description") = CStr(services(serviceId).description)
cmd.Parameters.Item("@uuid") = CStr(sUUID)
cmd.Parameters.Item("@Service_ID") = 0
cmd.Execute
Debug.Print Err.description
newService_ID = cmd.Parameters("@Service_ID").Value

How to shutdown reboot logoff WIndows 9x NT Me 2000

program shutdown;
{$APPTYPE CONSOLE}
uses
  SysUtils,
  Windows;

var
   logoff: boolean = false;
   reboot: boolean = false;
   warn: boolean = false;
   downQuick: boolean = false;
   cancelShutdown: boolean = false;
   powerOff: boolean = false;
   timeDelay: integer = 0;

function HasParam(Opt: Char): Boolean;
var
   x: integer;
begin
     result := false;
     for x := 1 to paramCount do
         if (paramstr(x) = '-'+opt) or (paramstr(x) = '/'+opt) then result := true;
end;

function GetErrorString: String;
var
   lz: Cardinal;
   err: array[0..512] of Char;
begin
     lz := GetLastError;
     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, lz, 0, @err, 512, nil);
     result := string(err);
end;

procedure DoShutdown;
var
   rl,flgs: Cardinal;
   hToken: Cardinal;
   tkp: TOKEN_PRIVILEGES;
begin
     flgs := 0;
     if downQuick then flgs := flgs or EWX_FORCE;
     if not reboot then flgs := flgs or EWX_SHUTDOWN;
     if reboot then flgs := flgs or EWX_REBOOT;
     if poweroff and (not reboot) then flgs := flgs or EWX_POWEROFF;
     if logoff then flgs := (flgs and (not (EWX_REBOOT or EWX_SHUTDOWN or EWX_POWEROFF))) or EWX_LOGOFF;
     if Win32Platform = VER_PLATFORM_WIN32_NT then begin
        if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
           Writeln('Cannot open process token. ['+GetErrorString+']')
        else begin
             if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) then begin
             tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
             tkp.PrivilegeCount := 1;
             AdjustTokenPrivileges(hToken, False, tkp, 0, nil, rl);
             if GetLastError <> ERROR_SUCCESS then
                   Writeln('Error adjusting process privileges.');
             end else Writeln('Cannot find privilege value. ['+GetErrorString+']');
        end;
{        if CancelShutdown then
           if AbortSystemShutdown(nil) = False then
              Writeln(\'Cannot abort. [\'+GetErrorString+\']\')
           else
               Writeln(\'Cancelled.\')
        else begin
             if InitiateSystemShutdown(nil, nil, timeDelay, downQuick, Reboot) = False then
                Writeln(\'Cannot go down. [\'+GetErrorString+\']\')
             else
                 Writeln(\'Shutting down!\');
        end;}
     end;
//     else begin
          ExitWindowsEx(flgs, 0);
//     end;
end;

begin
     Writeln('Shutdown v0.3 for Win32 (similar to the Linux version)');
     Writeln('X Software. All Rights Reserved.');
     if HasParam('?') or (ParamCount=0) then begin
        Writeln('Usage:    shutdown [-akrhfnc] [-t secs]');
        Writeln('                  -k:      don''t really shutdown, only warn.');
        Writeln('                  -r:      reboot after shutdown.');
        Writeln('                  -h:      halt after shutdown.');
        Writeln('                  -p:      power off after shutdown');
        Writeln('                  -l:      log off only');
        Writeln('                  -n:      kill apps that don''t want to die.');
        Writeln('                  -c:      cancel a running shutdown.');
     end else begin
         if HasParam('k') then warn := true;
         if HasParam('r') then reboot := true;
         if HasParam('h') and reboot then begin
            Writeln('Error: Cannot specify -r and -h parameters together!');
            Exit;
         end;
         if HasParam('h') then reboot := false;
         if HasParam('n') then downQuick := true;
         if HasParam('c') then cancelShutdown := true;
         if HasParam('p') then powerOff := true;
         if HasParam('l') then logoff := true;
         DoShutdown;
     end;
end.

Volumes & Areas

/PROGRAM TO CALCULATE AREA,VOLUME,PERIMETER OF A PARTICULAR
 GEOMETRIC SHAPE/

#include
#include
#include
#define PI 3.14159
char ch;
main()
 {

clrscr();

  textcolor(4);
  intro();
  getch();
  textcolor(7);
  clrscr();
  do
   {
      ch=menu();
switch(ch)
{
  case 'a':
  case 'A':
      clrscr();
      square();
      getch();
      break;
  case 'b':
  case 'B':
      clrscr();
      rect();
      getch();
      break;
  case 'c':
  case 'C':
      clrscr();
      circl();
      getch();
      break;
  case 'd':
  case 'D':
      clrscr();
      tri();
      getch();
      break;
  case 'e':
  case 'E':
      clrscr();
      rom();
      getch();
      break;
  case 'f':
  case 'F':
      clrscr();
      para();
      getch();
      break;

  case 'g':
  case 'G':
      clrscr();
      tra();
      getch();
      break;
  case 'h':
  case 'H':
      clrscr();
      qua();
      getch();
      break;
  case 'i':
  case 'I':
      clrscr();
      semicir();
      getch();
      break;
  case 'j':
  case 'J':
      clrscr();
      msector();
      getch();
      break;

  case 'k':
  case 'K':
                      clrscr();
     sphere();
     getch();
     break;
  case 'l':
  case 'L':
      clrscr();
      cone();
      getch();
      break;
  case 'm':
  case 'M':
      clrscr();
      cyll();
      getch();
      break;

  case 'n':
  case 'N':
      clrscr();
      cube();
      getch();
      break;
  case 'o':
  case 'O':
      clrscr();
      cuboid();
      getch();
      break;
  case 'p':
  case 'P':
      clrscr();
      hemisphe();
      getch();
      break;

  case 'q':
  case 'Q':
      exit(1);
}
   } while(ch!='Q'||ch!='q');
      getch();
 }
  intro()
   {
     int i;
     clrscr();
     printf("



");
     textcolor(2);


cprintf("#################################################################
###############");
     textcolor(4);
     printf("



  PROGRAM TO CALCULATE AREAS , VOLUMES ,
CIRCUMFERENCES ");
     printf("
 
=====================================================
");
     printf("
   OF VARIOUS GEOMETRIC SHAPES");
     printf("
   ===========================

");
     textcolor(2);

cprintf("#################################################################
###############");
     getch();

     printf("





 Program developed and designed
by...

");
     printf("WWW");

   }
  menu()
   {
      clrscr();
      textcolor(7);
      printf(" MENU
Two Dimensional Shapes.
 
-----------------------
 
A.SQUARE
B.RECTANGLE
 
C.CIRCLE
D.TRIANGLE
 
E.RHOMBUS
F.PARALLELOGRAM
 
G.TRAPEZIUM
H.QUADRILATERAL.
 
I.SEMICERCLE
J.SECTOR
");
      printf("
Three Dimensional Shapes.
     
-------------------------
     
K.SPHERE
L.CONE
M.CYLLINDER
     
N.CUBE
O.CUBOID
P.HEMISPHERE
     
Q.QUIT
Enter Your Choice :");
      scanf("%c",&ch);
     return(ch);
   }

  /*****   SUB FUNCTIONS  *****/
  /*****    2 D SHAPES    *****/

   square()
    {
      float s,a,p;int i,j;
      printf("
 Enter side of square:");
      scanf("%f",&s);
      a=s*s;
      p=4*s;
      printf("
Perimeter of square  : %.3f units",p);
      printf("
Area of square       : %.3f sq.units",a);
      printf("
 Square is ...
");
      for(i=1;i<=s;i++)
{
  textcolor(10);
  for(j=1;j<=s;j++)
   cprintf("ÛÛ");
   printf("
");
 }
      return(0);
    }
   rect()
    {
     float a,p,l,b;   int i,j;
      printf("
 Enter length and breadth of rectangle:
Length:");
      scanf("%f",&l);
      printf("
Breadth:");
      scanf("%f",&b);
      a=l*b;
      p=2*(l+b);
      printf("
Perimeter of rectangle  : %.3f units",p);
      printf("
Area of rectangle       : %.3f sq.units",a);
      printf("
 Rectangle is...
");
      for(i=1;i<=b;i++)
{
  textcolor(4);
  for(j=1;j<=l;j++)
   cprintf("ÛÛ");
   printf("
");
 }
      return(0);
    }
  tri()
   {
    float area,p;
    float a,b,c,s;
    printf("
Enter three sides of triangle:");
    scanf( "%f%f%f",&a,&b,&c);
    p=a+b+c;
    s=p/2;
    area=sqrt(s*(s-a)*(s-b)*(s-c));
    printf("
Perimeter of triangle : %.3f units",p);
    printf("
Area of a triangle    : %.3f sq.units",area);
   }
  rom()
   {
     float s,d1,d2,a,p;
     printf("
Enter side and diagonals of a rhombus:
Side:");
     scanf("%f",&s);
     printf("
Diagonal :");scanf("%f",&d1);
     printf("
Diagonal :");scanf("%f",&d2);
     a=0.5*d1*d2;
     p=4*s;
     printf("
Perimeter of rhombus   :%.3f units",p);
     printf("
Area of rhombus        :%.3f sq.units",a);
   }
  circl()
   {
    float r,a,p;
    printf("Enter radius of circle:");
    scanf("%f",&r);
    a=PI * r * r;
    p=2 * PI * r;
    printf("
Circumference of circle : %.3f units",p);
    printf("
Area of circle          : %.3f sq.units",a);
   }
  para()
   {
    float a,p,base,h,l,b;
    printf("Enter height,length,breadth of parallalogram :
" );
    printf("
Height :"); scanf("%f",&h);
    printf("
Base or Length :"); scanf("%f",&l);
    printf("
Breadth :"); scanf("%f",&b);
    base=l;
    a=base*h;
    p=2 * ( l + b );
    printf("
Perimeter of parallalogram :%.3f units",p);
    printf("
Area of parallogram        :%.3f sq.units",a);

   }


  tra()
   {
    float a,b,d,are;
    printf("Enter height and lengths of two parallel sides:
Height :");
    scanf("%f",&d);
    printf("Side:"); scanf("%f",&a);
    printf("Side:"); scanf("%f",&b);
    are=0.5 * d * (a+b);
    printf("
Area of trapezium : %.3f sq.units",are);
   }
 qua()
  {
   float a,b,area,d;
   printf("Enter diagonal and perpendicular distances from opposite
vertices:
");
   printf("Diagonal :"); scanf("%f",&d);
   printf("
Distance :"); scanf("%f",&a);
   printf("
Distance :");scanf("%f",&b);
   area= 0.5 * d * (a + b);
   printf("
Area of quadrilateral : %.3f sq.units", area);
  }
 semicir()
  {
    float a,p,r;
    printf("Enter radius of semicircle:");
    scanf("%f",&r);
    a=0.5* PI * r * r;
    p= (PI * r ) + (2 * r);
    printf("
Circumference of semicircle : %.3f units",p);
    printf("
Area of semicircle          : %.3f sq.units",a);
  }

 msector()
  {
    float x,r,temp,a,p;
    printf("Enter radius and angle of sector:");
    printf("
Radius :");
    scanf("%f",&r);
    printf("
Angle(in degrees) :");
    scanf("%f",&x);
    temp= x/360;
    a= temp * (PI * r * r);
    p= temp * (2 * PI * r);
    printf("
Circumference of sector : %.3f units",p);
    printf("
Area of sector          : %.3f sq.units",a);
  }

       /******** 3 DIMENSIONAL SHAPES  *********/

  sphere()
   {
     float lsa,tsa,v,r;
     printf("Enter radius of sphere :");
     scanf("%f",&r);
     tsa=4*PI*r*r;
     v=(4.0/3.0)*PI*r*r*r;
     printf("
Total surface area of sphere   :%.3f sq.units",tsa);
     printf("
Volume of sphere               :%.3f cu.units",v);
   }
  cone()
   {
    float h,r,s ,v,tsa,lsa;
    printf("Enter base radius ,height, slant height of cone :");
    printf("
Radius :"); scanf("%f",&r);
    printf("
Height :"); scanf("%f",&h);
    printf("
Slant height :"); scanf("%f",&s);
    tsa=PI * r *(s+r);
    lsa=PI * r * s;
    v=(PI * r * r * h)/3;
    printf("
Total surface area of cone    :%.3f sq.units",tsa);
    printf("
Lateral surface area of cone  :%.3f sq.units",lsa);
    printf("
Volume of cone                :%.3f cu.units",v);
   }
  cyll()
   {
      float lsa,tsa,v,r,h;
      printf("Enter height and radius of cyllinder");
      printf("Height :"); scanf("%f",&h);
      printf("Radius :"); scanf("%f",&r);
      lsa=2*PI*r*h;
      tsa=2*PI*r*(h+r);
      v=PI*r*r*h;
      printf("
Total surface area of cyllinder  :%.3f sq.units",tsa);
      printf("
Curved surface area of cyllinder :%.3f sq.units",lsa);
      printf("
Volume of cyllinder              :%.3f cu.units",v);
   }
  cube()
   {
     float  lsa,tsa,v,s,d;
     printf("Enter side of cube :");
     scanf("%f",&s);
     d=s*sqrt(3);
     lsa=4 * s * s;
     tsa=6 * s * s;
     v= s * s * s;
     printf("
Diagonal of cube              :%.3f units",d);
     printf("
Total surface area of cube    :%.3f sq.units",tsa);
     printf("
Lateral surface area of cube  :%.3f sq.units",lsa);
     printf("
Volume of cube                :%.3f cu.units",v);
   }
  cuboid()
   {
    float lsa,tsa,v,l,b,d,h;
    printf("Enter length,breadth,height of cuboid :");
    printf("
Length :");  scanf("%f",&l);
    printf("
Breadth :");  scanf("%f",&b);
    printf("
Height :");  scanf("%f",&h);
    d=sqrt(l*l + b*b + h*h );
    lsa =2 * h *( l+b );
    tsa = lsa + 2 * l * b;
    v=l*b*h;
    printf("
Diagonal of cuboid              :%.3f units",d);
    printf("
Total surface area of cuboid    :%.3f sq.units",tsa);
    printf("
Lateral surface area of cuboid  :%.3f sq.units",lsa);
    printf("
Volume of cuboid                :%.3f cu.units",v);
   }
  hemisphe()
   {
             float lsa,tsa,v,r;
     printf("Enter radius of hemisphere :");
     scanf("%f",&r);
     tsa=3*PI*r*r;
     lsa=2*PI*r*r;
     v=(2.0/3.0)*PI*r*r*r;
     printf("
Total surface area of hemisphere    :%.3f sq.units",tsa);
     printf("
Lateral surface area of hemisphere  :%.3f sq.units",lsa);
     printf("
Volume of hemisphere                :%.3f cu.units",v);
   }

Factorial series-e^x

#include
#include
#include
long int factorial(int n);
void main()
{
 int x,i;
 float s,r;
 char c;
 clrscr();
printf("You have this series:-1+x/1! + x^2/2! + x^3/3! + x^4/4!
..x^x/x!");
 printf("
To which term you want its sum?  ");
 scanf("%d",&x);
 s=0;
 for (i=1;i<=x;i++)
  {   s=s+((float)pow(x,i)/(float)factorial(i)); }
 printf("The sum of %d terms is %f",x,1+s);
 fflush(stdin);
      getch();
}

long int factorial(int n)
 {
  if (n<=1)
return(1);
  else
n=n*factorial(n-1);
return(n);
 }

Square Root of a number by using simple calculations

#include
#include
main()
 {
  float a,b,e=0.00001,p,k;
  clrscr();
    textcolor(GREEN);
 do {
     printf("     ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
");
     printf("     xDB     PROGRAM TO FIND SQUARE ROOT OF A NUMBERxDB
");
     printf("     ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
");
    cprintf("
ENTER A NUMBER(-1 to Quit) :");
    scanf("%f",&k);

  a=k;p=a*a;
  while(p-k>=e)
   {
    b=(a+(k/a))/2;
    a=b;
    p=a*a;
   }
  printf("SQUARE ROOT IS =  %f
",a);
  getch();
  clrscr();
 }while(k!=-1);
  getch();
 }

Program to find your Day of Birth given Date of Birth

#include
#include
#include

main()
{
 clrscr();
 int d,m,y,year,month,day,i,n;
 printf("Enter how many times you want to run this program : ");
 scanf("%d",&n);
 for(i=1;i<=n;i++)
 {
 printf("

Enter the date : ");
 scanf("%d%d%d",&d,&m,&y);
 if( d>31 || m>12 || (y<1900 || y>=2000) )
  {
   printf("

INVALID INPUT
");
   getch();
   exit(0);
  }
 year = y-1900;
 year = year/4;
 year = year+y-1900;
 switch(m)
  {
   case 1:
   case 10:
   month = 1;
   break;
   case 2:
   case 3:
   case 11:
   month = 4;
   break;
   case 7:
   case 4:
  month = 0;
  break;
   case 5:
  month = 2;
  break;
   case 6:
  month = 5;
  break;
   case 8:
  month = 3;
  break;
   case 9:
   case 12:
  month = 6;
  break;
  }
 year = year+month;
 year = year+d;
 day  = year%7;
 switch(day)
  {
   case 0:
  printf("

Day is SATURDAY
");
  break;
   case 1:
  printf("

Day is SUNDAY
");
  break;
   case 2:
  printf("

Day is MONDAY
");
  break;
   case 3:
  printf("

Day is TUESDAY
");
  break;
   case 4:
  printf("

Day is WEDNESDAY
");
  break;
   case 5:
  printf("

Day is THURSDAY
");
  break;
   case 6:
  printf("

Day is FRIDAY
");
  break;
  }
}
 getch();
 return 0;
}

Program to calculate Area of a Polygon

/* Given the coordinates of the vertices of a convex polygon,
   calculate its area and perimeter. Subdivide it into triangles
   and calculate the area of each triangle with Heron's formula.
   Requires data file pvert.txt containing coordinates of each vertex.
   Example of data for a polygon with 5 vertices:

   3 7 6 4 3 -2 -6 1 -6 7
*/
#include
#include
#include
#define MAX_VERT 50
enum {x, y};
typedef struct triangle {
    double v1[2];
    double v2[2];
    double v3[2];
} triangle;
double area(triangle a);
double perimeter(double *vertices, int size);
double side(double *p1, double *p2);

int main(void)
{
  int n, idx;
  int triangles;
  int index;
  int xycount;
  double xy;
  double triangle_area;
  double polygon_area;
  double perim;
  double polygon_vertices[MAX_VERT] = {0.0};
  triangle a;
  FILE *data;

  xycount = 0;
  polygon_area = 0;
  if((data = fopen("pvert.txt", "r")) == NULL)
  {
    fprintf(stderr, "can't open data file
");
    exit(EXIT_FAILURE);
  }

  /* Read x-y coordinates of the vertices
     of the polygon from a file. */
  while(fscanf(data, "%lf", &xy) == 1)
    polygon_vertices[xycount++] = xy;
  fclose(data);
  idx = 0;
  /* triangles in polygon = vertices - 2 */
  triangles = (xycount / 2) - 2;
  putchar('
');

  for(index = 2, idx = 0; idx < triangles; index += 2, ++idx)
  {
  /* Load vertices of a triangle into struct.
     1st vertex of the polygon will be the 1st
     vertex of each triangle. index holds the
     starting index of each consecutive set of
     triangle vertices after the 1st. */
    a.v1[x] = polygon_vertices[0];
    a.v1[y] = polygon_vertices[1];
    a.v2[x] = polygon_vertices[index+0];
    a.v2[y] = polygon_vertices[index+1];
    a.v3[x] = polygon_vertices[index+2];
    a.v3[y] = polygon_vertices[index+3];

    /* calculate the area of the triangle */
    triangle_area = area(a);
    printf("area of triangle = %.2f
", triangle_area);

    /* add triangle area to polygon area */
    polygon_area += triangle_area;
  }
  printf("
area of polygon = %.2f
", polygon_area);

  /* calculate the perimeter of the polygon */
  perim = perimeter(polygon_vertices, xycount);
  printf("perimeter of polygon = %.2f
", perim);

  return 0;
}

/* calculate triangle area with Heron's formula */
double area(triangle a)
{
  double s1, s2, s3, S, area;

  s1 = side(a.v1, a.v2);
  s2 = side(a.v2, a.v3);
  s3 = side(a.v3, a.v1);
  S = (s1 + s2 + s3) / 2;
  area = sqrt(S*(S - s1)*(S - s2)*(S - s3));

  return area;
}

/* calculate polygon perimeter */
double perimeter(double *vertices, int size)
{
  int idx, jdx;
  double p1[2], p2[2], pfirst[2], plast[2];
  double perimeter;

  perimeter = 0.0;
  /* 1st vertex of the polygon */
  pfirst[x] = vertices[0];
  pfirst[y] = vertices[1];
  /* last vertex of polygon */
  plast[x] = vertices[size-2];
  plast[y] = vertices[size-1];
  /* calculate perimeter minus last side */
  for(idx = 0; idx <= size-3; idx += 2)
  {
    for(jdx = 0; jdx < 4; ++jdx)
    {
      p1[x] = vertices[idx];
      p1[y] = vertices[idx+1];
      p2[x] = vertices[idx+2];
      p2[y] = vertices[idx+3];
    }
    perimeter += side(p1, p2);
  }
  /* add last side */
  perimeter += side(plast, pfirst);

  return perimeter;
}

/* calculate length of side */
double side(double *p1, double *p2)
{
  double s1, s2, s3;

  s1 = (p1[x] - p2[x]);
  s2 = (p1[y] - p2[y]);
  s3 = (s1 * s1) + (s2 * s2);

  return sqrt(s3);
}

Program for Decimal to Roman Number conversion

#include

main()
{
int a,b,c,d,e;
clrscr();
printf("Input a  number (between 1-3000):");
scanf("%d",&e);
while (e==0||e>3000)
       {
       printf ("ERROR: Invalid Input!
");
       printf ("Enter the number again:");
       scanf ("%d",&e);
       }
if (e>3000)
printf("Invalid");
a = (e/1000)*1000;
b = ((e/100)%10)*100;
c = ((e/10)%10)*10;
d = ((e/1)%10)*1;

if (a ==1000)
printf("M");
else if (a ==2000)
printf("MM");
else if (a ==3000)
printf("MMM");

if (b == 100)
printf("C");
else if (b == 200)
printf("CC");
else if (b == 300)
printf("CCC");
else if (b == 400)
printf("CD");
else if (b ==500)
printf("D");
else if (b == 600)
printf("DC");
else if (b == 700)
printf("DCC");
else if (b ==800)
printf("DCCC");
else if (b == 900)
printf("CM");


if (c == 10)
printf("X");
else if (c == 20)
printf("XX");
else if (c == 30)
printf("XXX");
else if (c == 40)
printf("XL");
else if (c ==50)
printf("L");
else if (c == 60)
printf("LX");
else if (c == 70)
printf("LXX");
else if (c ==80)
printf("LXXX");
else if (c == 90)
printf("XC");

if (d == 1)
printf("I");
else if (d == 2)
printf("II");
else if (d == 3)
printf("III");
else if (d == 4)
printf("IV");
else if (d ==5)
printf("V");
else if (d == 6)
printf("VI");
else if (d == 7)
printf("VII");
else if (d ==8)
printf("VIII");
else if (d == 9)
printf("IX");
getch();
}

Progam that gives all details of a Triangle given the lengths of its sides

#include
#include
#include
#include

main()
{
 clrscr();
 float a,b,c,S,D,A,B,C,Area,R;
 printf("Enter the lengths of the three sides of the triangle :

");
 scanf("%f%f%f",&a,&b,&c);

 S = (a+b+c)/2.0;        // S is the semiperimeter of the triangle
 D = S*(S-a)*(S-b)*(S-c);//D is the square of the area of the triangle
 if(D<=0)
 {
  printf("
The triangle cannot be formed");
  getch();
  exit(0);
 }

 if((a==b || b==c || c==a) && !(a==b && b==c && c==a))
// this complex logic is to eliminate interpretting a triangle with all
three
// sides equal as both isosceles and equilateral.
   printf("
The triangle is ISOSCELES

");
 if(a==b && b==c && c==a)
   printf("
The triangle is EQUILATERAL

");
 if(a!=b && b!=c && c!=a)
  printf("
The triangle is SCALENE

");

 Area = sqrt(D);
 R = (a*b*c)/(4.0*Area);
 printf("PERIMETER     = %.2f units
",(2.0*S));
 printf("AREA          = %.2f sq.units
",Area);
 printf("CIRCUM RADIUS = %.2f units
",R);
// using sine rule,we get...
 A = (180.0/3.1415926)*asin(a/(2.0*R));//  value of pi should be upto 7
 B = (180.0/3.1415926)*asin(b/(2.0*R));//  decimal places of accuracy and
also
 C = (180.0/3.1415926)*asin(c/(2.0*R));//  note that the 7th decimal place
is
       //  6 and not 7 as it had to be if were
 if(A==90.0 || B==90.0 || C==90.0)     //  approximated to 7 decimal
places
  printf("
The triangle is RIGHT ANGLED
");
 if(A<90.0 && B<90.0 && C<90.0)
  printf("
The triangle is ACUTE ANGLED
");
 if(A>90.0 || B>90.0 || C>90.0)
  printf("
The triangle is OBTUSE ANGLED
");

 printf("
The angles are as follows :

");
 printf("A = %.2f degrees
",A);
 printf("B = %.2f degrees
",B);
 printf("C = %.2f degrees
",C);
 printf("
Where A,B,C stand for angles opposite to sides
%.2f,%.2f,%.2f",a,b,c);
 printf(" respectively
");


 getch();
 return 0;
}

Circle Through Three Points

#include
#include
#include

int main()
{
clrscr();
double f,g,m,x1,x2,x3,y1,y2,y3;
double c,d,h,e,k,r,s;
for(;;)
{
if(scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3)==EOF)
//checking for input
break;

f = x3*x3-x3*x2-x1*x3+x1*x2+y3*y3-y3*y2-y1*y3+y1*y2; //formula
g = x3*y1-x3*y2+x1*y2-x1*y3+x2*y3-x2*y1;

if(g==0)
  m = 0;
else
  m = (f/g);

c = (m*y2)-x2-x1-(m*y1); //formula
d = (m*x1)-y1-y2-(x2*m);
e = (x1*x2)+(y1*y2)-(m*x1*y2)+(m*x2*y1);

h = (c/2); //formula
k = (d/2);
s = (((h)*(h))+((k)*(k))-e);
r = pow(s,.5);

printf("(x");

if(h>=0)
  printf(" + ");
else if(h<0)
  printf(" - ");
if(h<0)
  h=-h;
printf("%.3lf)^2",(h));
printf(" + ");
printf("(y");
if(k>=0)
  printf(" + ");
else if(k<0)
  printf(" - ");
if(k<0)
  k=-k;
printf("%.3lf)^2 = %.3lf^2",(k),r);

printf("
");

printf("x^2 + y^2");

if(c>=0) printf(" + ");
else if(c<0) printf(" - ");

if(c<0) c=-c;
printf("%.3lfx",c);

if(d>=0) printf(" + ");
else if(d<0) printf(" - ");

if(d<0) d=-d;
printf("%.3lfy",d);

if(e>=0) printf(" + ");
else if(e<0) printf(" - ");

if(e<0) e=-e;
printf("%.3lf = 0",e);
printf("

");
}

getch();
return 0;
}

Treesort [string array]

#include

#include

#include


struct tnode { 

 char *str;

 struct tnode *left; 

 struct tnode *right; 

};


void insert(struct tnode **p, char *value);

void print(struct tnode *root);


int main(void) {

 char line[1024];

 struct tnode *root;


 root = NULL;

 while((fgets(line, 1024, stdin)) != NULL)

  insert(&root, line);


 print(root);

 return 0;

}


/* call by reference .. ! */

void insert(struct tnode **p, char *value) {

 if(!*p) {

  *p = (struct tnode *)malloc(sizeof(struct tnode));

  (*p)->left = (*p)->right = NULL;

  (*p)->str = strdup(value);

  return;

 }


 if(strcmp(value, (*p)->str) < 0)

  insert(&(*p)->left, value);

 else 

  insert(&(*p)->right, value);

}

 

/* inorder binary tree print ... */

void print(struct tnode *root) {

 if(root != NULL) {

  print(root->left);

  printf("%s", root->str); 

  print(root->right);

 }

}


Ssort, selection sort [array]

#include

void selection_sort(int a[], int size);

int main(void) {
 int arr[10] = {10, 2, 4, 1, 6, 5, 8, 7, 3, 9};
 int i = 0;

 printf("before:\n");
 for(i = 0; i < 10; i++) printf("%d ", arr[i]);
 printf("\n");

 selection_sort(arr, 10);

 printf("after:\n");
 for(i = 0; i < 10; i++) printf("%d ", arr[i]);
 printf("\n");

 return 0;
}

void selection_sort(int a[], int size) {
 int i = 0;
 int j = 0;
 int large = 0;
 int index = 0;

 for(i = size - 1; i > 0; i--) {
  large = a[0];
  index = 0;
  for(j = 1; j <= i; j++)
   if(a[j] > large) {
    large = a[j];
    index = j;
   }
   a[index] = a[i];
   a[i] = large;
 }
}