# Turbo C / C++ - Find Square Root With Algorithm

Here is the Turbo C and C++ source code for finding square root by using algorithm and not using math.h header file.

## Source Code

#include <stdio.h>

#include <math.h>

double MySqrt(double x)

{

long numeric = (long) x;

long n = numeric;

long fraction =(long) ((x - numeric) * 1000000); // 6 digits

long f = fraction;

int numdigits = 0, fnumdigits = 0, currdigits = 0;

int tempresult = 0;

int bOdd = 0, part = 0, tens = 1;

int fractioncount = 0;

double result = 0;

int k, f1, f2, i, num, temp, quotient;

for(numdigits = 0; n >= 10; numdigits++)

n = (n / 10);

numdigits++;

for(fnumdigits = 0; f >= 10; fnumdigits++)

f = (f / 10);

fnumdigits++;

if( (numdigits % 2) == 1)

bOdd = 1;

while(1)

{

tens = 1;

currdigits = (bOdd == 1) ? (numdigits - 1) : (numdigits - 2);

for(k = 0; k < currdigits; k++)

tens *= 10;

part = numeric / tens;

// Get the Nearest Multiplication Factor

num = part;

quotient = tempresult * 2;

i = 0, temp = 0;

for(i = 1; ;i++)

{

if(quotient == 0)

{

if(num - i * i < 0)

{

tempresult = (i - 1);

break;

}

}

else

{

temp = quotient * 10 + i;

if(num - i * temp < 0)

{

tempresult = quotient / 2 * 10 + i - 1;

break;

}

}

}

// Done with Nearest Multiplication Factor

f1 = tempresult / 10;

f2 = tempresult % 10;

if(f1 == 0)

numeric = numeric - (tempresult * tempresult * tens);

else

numeric = numeric - ((f1 * 2 * 10 + f2) * f2 * tens);

if(numeric == 0 && fraction == 0)

{

if(currdigits > 0)

{

// Handle the Zero case

tens = 1;

currdigits = currdigits / 2;

for(k = 0; k < currdigits; k++)

tens *= 10;

tempresult *= tens;

}

break;

}

if(bOdd == 1)

{

numdigits -= 1;

bOdd = 0;

}

else

numdigits -= 2;

if( numdigits <= 0)

{

if(numeric > 0 || fraction > 0)

{

if(fractioncount >= 5)

break;

// Handle the fraction part for integer numbers

fractioncount++;

numeric *= 100;

if(fraction > 0)

{

// Handle the fraction part for real numbers

fnumdigits -= 2;

tens = 1;

for(k = 0; k < fnumdigits; k++)

tens *= 10;

numeric += fraction / tens;

fraction = fraction % tens;

}

numdigits += 2;

}

else

break;

}

}

if(fractioncount == 0)

result = tempresult;

else

{

tens = 1;

for(k = 0; k < fractioncount; k++)

tens *= 10;

result = (double) tempresult / tens;

}

return result;

}

int main()

{

double d = 0;

for(d = 1; d < 250000; d += 1234.56979)

printf("sqrt(%.5lf) = %.5lf %.5lf\n", d, MySqrt(d), sqrt(d));

d = 250000;

printf("sqrt(%.5lf) = %.5lf %.5lf\n", d, MySqrt(d), sqrt(d));

return 0;

}

## Output

sqrt(1.00000) = 1.00000 1.00000
sqrt(1235.56979) = 35.15067 35.15067
sqrt(2470.13958) = 49.70049 49.70050
sqrt(3704.70937) = 60.86632 60.86632
sqrt(4939.27916) = 70.28000 70.28001
sqrt(6173.84895) = 78.57384 78.57384
sqrt(7408.41874) = 86.07217 86.07217
sqrt(8642.98853) = 92.96767 92.96767
sqrt(9877.55832) = 99.38590 99.38591
sqrt(11112.12811) = 105.41407 105.41408
sqrt(12346.69790) = 111.11569 111.11570
sqrt(13581.26769) = 116.53869 116.53870
sqrt(14815.83748) = 121.72032 121.72032
sqrt(16050.40727) = 126.69020 126.69020
sqrt(17284.97706) = 131.47234 131.47234
sqrt(18519.54685) = 136.08654 136.08654
sqrt(19754.11664) = 140.54933 140.54934
sqrt(20988.68643) = 144.87472 144.87473
sqrt(22223.25622) = 149.07466 149.07467
sqrt(23457.82601) = 153.15947 153.15948
sqrt(24692.39580) = 157.13814 157.13814
sqrt(25926.96559) = 161.01852 161.01853
sqrt(27161.53538) = 164.80757 164.80757
sqrt(28396.10517) = 168.51143 168.51144
sqrt(29630.67496) = 172.13562 172.13563
sqrt(30865.24475) = 175.68507 175.68507
sqrt(32099.81454) = 179.16421 179.16421
sqrt(33334.38433) = 182.57706 182.57706
sqrt(34568.95412) = 185.92728 185.92728
sqrt(35803.52391) = 189.21819 189.21819
sqrt(37038.09370) = 192.45502 192.45284
sqrt(38272.66349) = 195.63400 195.63400
sqrt(39507.23328) = 198.76426 198.76427
sqrt(40741.80307) = 201.84598 201.84599
sqrt(41976.37286) = 204.88136 204.88136
sqrt(43210.94265) = 207.87241 207.87242
sqrt(44445.51244) = 210.82104 210.82104
sqrt(45680.08223) = 213.73072 213.72899
sqrt(46914.65202) = 216.59790 216.59790
sqrt(48149.22181) = 219.42930 219.42931
sqrt(49383.79160) = 222.22464 222.22464
sqrt(50618.36139) = 224.98524 224.98525
sqrt(51852.93118) = 227.71238 227.71239
sqrt(53087.50097) = 230.40725 230.40725
sqrt(54322.07076) = 233.07232 233.07096
sqrt(55556.64055) = 235.70456 235.70456
sqrt(56791.21034) = 238.30906 238.30906
sqrt(58025.78013) = 240.88540 240.88541
sqrt(59260.34992) = 243.43448 243.43449
sqrt(60494.91971) = 245.95715 245.95715
sqrt(61729.48950) = 248.45420 248.45420
sqrt(62964.05929) = 250.92746 250.92640
sqrt(64198.62908) = 253.37448 253.37448
sqrt(65433.19887) = 255.79913 255.79914
sqrt(66667.76866) = 258.20102 258.20102
sqrt(67902.33845) = 260.58077 260.58077
sqrt(69136.90824) = 262.93898 262.93898
sqrt(70371.47803) = 265.27622 265.27623
sqrt(71606.04782) = 267.59386 267.59306
sqrt(72840.61761) = 269.89001 269.89001
sqrt(74075.18740) = 272.16757 272.16757
sqrt(75309.75719) = 274.42623 274.42623
sqrt(76544.32698) = 276.66645 276.66645
sqrt(77778.89677) = 278.88868 278.88868
sqrt(79013.46656) = 281.09334 281.09334
sqrt(80248.03635) = 283.28142 283.28084
sqrt(81482.60614) = 285.45158 285.45158
sqrt(82717.17593) = 287.60593 287.60594
sqrt(83951.74572) = 289.74427 289.74428
sqrt(85186.31551) = 291.86694 291.86695
sqrt(86420.88530) = 293.97429 293.97429
sqrt(87655.45509) = 296.06663 296.06664
sqrt(88890.02488) = 298.14467 298.14430
sqrt(90124.59467) = 300.20758 300.20759
sqrt(91359.16446) = 302.25678 302.25679
sqrt(92593.73425) = 304.29218 304.29219
sqrt(93828.30404) = 306.31406 306.31406
sqrt(95062.87383) = 308.32267 308.32268
sqrt(96297.44362) = 310.31829 310.31829
sqrt(97532.01341) = 312.30135 312.30116
sqrt(98766.58320) = 314.27151 314.27151
sqrt(100001.15299) = 316.22958 316.22959
sqrt(101235.72278) = 318.17561 318.17562
sqrt(102470.29257) = 320.10981 320.10981
sqrt(103704.86236) = 322.03239 322.03239
sqrt(104939.43215) = 323.94356 323.94356
sqrt(106174.00194) = 325.84381 325.84352
sqrt(107408.57173) = 327.73246 327.73247
sqrt(108643.14152) = 329.61059 329.61059
sqrt(109877.71131) = 331.47807 331.47807
sqrt(111112.28110) = 333.33508 333.33509
sqrt(112346.85089) = 335.18181 335.18182
sqrt(113581.42068) = 337.01842 337.01843
sqrt(114815.99047) = 338.84508 338.84508
sqrt(116050.56026) = 340.66194 340.66194
sqrt(117285.13005) = 342.46916 342.46917
sqrt(118519.69984) = 344.26690 344.26690
sqrt(119754.26963) = 346.05529 346.05530
sqrt(120988.83942) = 347.83450 347.83450
sqrt(122223.40921) = 349.60464 349.60465
sqrt(123457.97900) = 351.36587 351.36588
sqrt(124692.54879) = 353.11832 353.11832
sqrt(125927.11858) = 354.86211 354.86211
sqrt(127161.68837) = 356.59737 356.59738
sqrt(128396.25816) = 358.32423 358.32424
sqrt(129630.82795) = 360.04281 360.04281
sqrt(130865.39774) = 361.75322 361.75323
sqrt(132099.96753) = 363.45559 363.45559
sqrt(133334.53732) = 365.15002 365.15002
sqrt(134569.10711) = 366.83662 366.83662
sqrt(135803.67690) = 368.51550 368.51550
sqrt(137038.24669) = 370.18677 370.18677
sqrt(138272.81648) = 371.85052 371.85053
sqrt(139507.38627) = 373.50687 373.50688
sqrt(140741.95606) = 375.15590 375.15591
sqrt(141976.52585) = 376.79772 376.79773
sqrt(143211.09564) = 378.43355 378.43242
sqrt(144445.66543) = 380.06008 380.06008
sqrt(145680.23522) = 381.68080 381.68080
sqrt(146914.80501) = 383.29467 383.29467
sqrt(148149.37480) = 384.90177 384.90177
sqrt(149383.94459) = 386.50219 386.50219
sqrt(150618.51438) = 388.09601 388.09601
sqrt(151853.08417) = 389.68428 389.68331
sqrt(153087.65396) = 391.26417 391.26417
sqrt(154322.22375) = 392.83867 392.83867
sqrt(155556.79354) = 394.40688 394.40689
sqrt(156791.36333) = 395.96889 395.96889
sqrt(158025.93312) = 397.52475 397.52476
sqrt(159260.50291) = 399.07455 399.07456
sqrt(160495.07270) = 400.61917 400.61836
sqrt(161729.64249) = 402.15624 402.15624
sqrt(162964.21228) = 403.68826 403.68826
sqrt(164198.78207) = 405.21448 405.21449
sqrt(165433.35186) = 406.73498 406.73499
sqrt(166667.92165) = 408.24982 408.24983
sqrt(167902.49144) = 409.75906 409.75907
sqrt(169137.06123) = 411.26343 411.26276
sqrt(170371.63102) = 412.76098 412.76099
sqrt(171606.20081) = 414.25378 414.25379
sqrt(172840.77060) = 415.74123 415.74123
sqrt(174075.34039) = 417.22336 417.22337
sqrt(175309.91018) = 418.70026 418.70026
sqrt(176544.47997) = 420.17196 420.17196
sqrt(177779.04976) = 421.63906 421.63853
sqrt(179013.61955) = 423.10001 423.10001
sqrt(180248.18934) = 424.55646 424.55646
sqrt(181482.75913) = 426.00793 426.00793
sqrt(182717.32892) = 427.45447 427.45448
sqrt(183951.89871) = 428.89613 428.89614
sqrt(185186.46850) = 430.33297 430.33297
sqrt(186421.03829) = 431.76542 431.76503
sqrt(187655.60808) = 433.19234 433.19235
sqrt(188890.17787) = 434.61497 434.61498
sqrt(190124.74766) = 436.03296 436.03297
sqrt(191359.31745) = 437.44635 437.44636
sqrt(192593.88724) = 438.85520 438.85520
sqrt(193828.45703) = 440.25953 440.25953
sqrt(195063.02682) = 441.65967 441.65940
sqrt(196297.59661) = 443.05484 443.05485
sqrt(197532.16640) = 444.44590 444.44591
sqrt(198766.73619) = 445.83263 445.83263
sqrt(200001.30598) = 447.21505 447.21506
sqrt(201235.87577) = 448.59321 448.59322
sqrt(202470.44556) = 449.96716 449.96716
sqrt(203705.01535) = 451.33707 451.33692
sqrt(204939.58514) = 452.70253 452.70253
sqrt(206174.15493) = 454.06404 454.06404
sqrt(207408.72472) = 455.42148 455.42148
sqrt(208643.29451) = 456.77488 456.77488
sqrt(209877.86430) = 458.12428 458.12429
sqrt(211112.43409) = 459.46973 459.46973
sqrt(212347.00388) = 460.81166 460.81125
sqrt(213581.57367) = 462.14886 462.14887
sqrt(214816.14346) = 463.48262 463.48262
sqrt(216050.71325) = 464.81255 464.81256
sqrt(217285.28304) = 466.13869 466.13870
sqrt(218519.85283) = 467.46107 467.46107
sqrt(219754.42262) = 468.77971 468.77972
sqrt(220988.99241) = 470.09466 470.09466
sqrt(222223.56220) = 471.40594 471.40594
sqrt(223458.13199) = 472.71358 472.71358
sqrt(224692.70178) = 474.01761 474.01762
sqrt(225927.27157) = 475.31807 475.31807
sqrt(227161.84136) = 476.61498 476.61498
sqrt(228396.41115) = 477.90837 477.90837
sqrt(229630.98094) = 479.19826 479.19827
sqrt(230865.55073) = 480.48470 480.48470
sqrt(232100.12052) = 481.76770 481.76770
sqrt(233334.69031) = 483.04729 483.04730
sqrt(234569.26010) = 484.32350 484.32351
sqrt(235803.82989) = 485.59636 485.59637
sqrt(237038.39968) = 486.86589 486.86589
sqrt(238272.96947) = 488.13212 488.13212
sqrt(239507.53926) = 489.39507 489.39507
sqrt(240742.10905) = 490.65477 490.65478
sqrt(241976.67884) = 491.91125 491.91125
sqrt(243211.24863) = 493.16452 493.16452
sqrt(244445.81842) = 494.41462 494.41462
sqrt(245680.38821) = 495.66156 495.66157
sqrt(246914.95800) = 496.90538 496.90538
sqrt(248149.52779) = 498.14609 498.14609
sqrt(249384.09758) = 499.38459 499.38372
sqrt(250000.00000) = 500.00000 500.00000