logo

Kvadrātsakne C

Šajā sadaļā tiks apspriesta dotā skaitļa kvadrātsaknes atrašana, izmantojot funkciju sqrt () programmēšanas valodā C. Matemātikā skaitļa kvadrātsakne ir tikai pretēja šī skaitļa kvadrātsakne. Tas nozīmē, kad to pašu reizina ar sevi, lai atgrieztu skaitļa kvadrātu. Un vienu skaitli, kas reizināts ar sevi, sauc par skaitļa kvadrātsakni. Piemēram, pieņemsim, ka mēs vēlamies iegūt jebkura skaitļa kvadrātu 3, skaitlis 3 tiek reizināts ar 3 * 3, lai atgrieztu kvadrātu 9. Un to pašu skaitli 3 sauc par skaitļa 9 kvadrātsakni. mums ir skaitlis 81, un šī skaitļa kvadrātsakne ir 9 (9 * 9 = 81).

Kvadrātsakne C

C programmēšanā funkcija sqrt () ir iepriekš definēta bibliotēkas funkcija, ko izmanto, lai aprēķinātu skaitļa kvadrātsakni. Funkcija sqrt() ir definēta math.h galvenes failā. Tātad, mums ir jāieraksta galvenes fails, vienlaikus izmantojot funkciju sqrt() C. Turklāt mēs varam atrast dotā skaitļa kvadrātsakni, neizmantojot funkciju sqrt.

Funkcijas sqrt() sintakse

 double sqrt( double arg); 

Iepriekš minētajā sintaksē funkcija sqrt() izmanto vienu argumentu kā dubultu, lai atgrieztu kvadrātsakni dubultos datu tipos.

arg: Tas ir funkcijas sqrt () dubultā datu tipa arguments.

Atgriešanas vērtība: Funkcija sqrt atgriež kvadrātsakni no dotā skaitļa definētā dubultā datu tipā.

java kolekciju ietvars

Piezīme. Mēs varam atrast int, float, dubultā vai garā dubultā datu tipa skaitļa kvadrātsakni, nepārprotami konvertējot doto datu tipu uz citu.

Algoritms kvadrātsaknes atrašanai

  1. Deklarē veselu skaitļu mainīgo kā num.
  2. Izmantojiet funkciju sqrt(), lai nodotu mainīgo num kā argumentu kvadrātsaknes atrašanai.
  3. Izdrukājiet rezultātu.
  4. Izejiet vai pārtrauciet programmu.

1. piemērs: Programma, lai iegūtu skaitļa kvadrātsakni, izmantojot funkciju sqrt().

Apskatīsim piemēru, kā aprēķināt kvadrātsakni no dotā skaitļa, izmantojot funkciju sqrt() valodā C.

 #include #include #include int main () { // declaration of the int, float and double variables int x, res; float y, res1; double z, res2; x = 289; // use the sqrt() function to return integer values res = sqrt(x); printf (' The square root of %d is: %d', x, res); // square root of float variable y = 12.25; // use the sqrt() function to return float values res1 = sqrt(y); printf (' 
 The square root of %.2f is: %.2f', y, res1); // square root of double variable z = 144.00; // use the sqrt() function to return double values res2 = sqrt(z); printf (' 
 The square root of %.2lf is: %.2lf', z, res2); return 0; } 

Izvade:

 The square root of 289 is: 17 The square root of 12.25 is: 3.50 The square root of 144.00 is: 12.00 

2. piemērs. Programma, lai paņemtu no lietotāja skaitli un iegūtu kvadrātsakni

Apskatīsim piemēru, kā izdrukāt skaitļa kvadrātsakni, ņemot ievadi no lietotāja, un pēc tam izmantot funkciju sqrt() valodā C.

kad tika izgudrots pirmais dators
 #include #include #include int main () { // declare an integer variable int x; double res; printf (' Enter any number to get the square root: '); scanf (' %d', &x); // use the sqrt() function to return integer values res = sqrt(x); printf (' 
 The square root of %d is: %.2lf', x, res); return 0; } 

Izvade:

 Enter any number to get the square root: 625 The square root of 625 is: 25.00 

3. piemērs: Programma kvadrātsaknes atrašanai, izmantojot lietotāja definētu funkciju

Izveidosim programmu, lai iegūtu dotā skaitļa kvadrātsakni, izmantojot lietotāja definētu funkciju programmēšanas valodā C.

 #include #include #include // function declaration double getSqr (int num); int main () { // declare an integer variable int x; double res; printf (' Enter any number to get the square root: '); scanf (' %d', &x); res = getSqr(x); // call the function printf (' 
 The square root of %d is: %.2lf', x, res); return 0; } // function definition double getSqr ( int num) { double getRes; // use sqrt() function to print the square root getRes = sqrt (num); return getRes; } 

Izvade:

 Enter any number to get the square root: 87 The square root of 87 is: 9.33 

pow() funkcija

Pow() ir iepriekš definēta math.h galvenes faila funkcija, lai aprēķinātu dotā skaitļa jaudu.

Funkcijas pow() sintakse

 int pow( arg, 0.5); 

Funkcijai pow() ir divi argumenti: pirmais arguments definē mainīgo, lai iegūtu jaudu vai dotā skaitļa kvadrātsakni, un 0,5 ir noklusējuma arguments, kas ir vienāds ar ½ vai 1/2 = 0,5.

4. piemērs. Programma, lai iegūtu skaitļa kvadrātsakni, izmantojot funkciju pow().

Apskatīsim piemēru, kā izdrukāt skaitļa kvadrātsakni, izmantojot funkciju pow() valodā C.

 #include #include #include int main () { // declare an integer variable int x; double res; printf (' Enter any number to get the square root: '); scanf (' %d', &x); // use the pow() function to return the square root res = pow(x, 0.5); //it takes two argument: input variable and 0.5 is default value printf (' 
 The square root of %d is: %.2lf', x, res); return 0; } 

Izvade:

 Enter any number to get the square root: 1225 The square root of 1225 is: 35.00 

Iepriekš minētajā programmā mēs no lietotāja ņemam mainīgā x veselu skaitļu vērtību 1225 un nododam x kā argumentu funkcijai pow(), lai atgrieztu dotā skaitļa jaudu vai kvadrātsakni.

5. piemērs. Programma, lai iegūtu skaitļa kvadrātsakni, neizmantojot funkciju sqrt().

Apskatīsim piemēru, kā izdrukāt skaitļa kvadrātsakni, neizmantojot iepriekš definēto sqrt() funkciju C.

 /* Display the square root of a number without using the sqrt() function in C. */ #include #include int main() { // declaration of the variables int num; float sqrt, temp; printf (' Enter a number to get the square root: '); scanf (' %d', &num); // divide the given number by 2 and store into sqrt sqrt = num / 2; temp = 0; // use while loop to continuously checks the sqrt is not equal to the temp while (sqrt != temp) // Initially temp is 0 and sqrt = num { temp = sqrt; // assign sqrt to temp sqrt = ( num / temp + temp) / 2; } printf (' 
 The square root of %d is %f', num, sqrt); return 0; } 

Izvade:

java cilpas
 Enter a number to get the square root: 2 The square root of 2 is 1.414214 

Iepriekš minētajā programmā mēs ievadām lietotāja skaitli, kura kvadrātsakni atrodam. Tātad, pirmkārt, mēs dalām doto skaitli ar 2 un saglabājam to mainīgajā sqrt. Pēc tam mēs inicializējam temp ar 0. Un tad izmantojiet while cilpu, kas nepārtraukti atkārtojas un pārbauda, ​​vai sqrt nav vienāds ar temp, un katrā iterācijā tā piešķir sqrt vērtību temp, un sqrt iegūst jaunu vērtību, atrisinot loģika (skaits/temp + temp) /2; Un tad izdrukā kvadrātsakni no 2 ir 1,414214.