logo

C의 제곱근

이 섹션에서는 C 프로그래밍 언어의 sqrt() 함수를 사용하여 주어진 숫자의 제곱근을 찾는 방법에 대해 설명합니다. 수학에서 숫자의 제곱근은 해당 숫자의 제곱의 역수입니다. 이는 숫자의 제곱을 반환하기 위해 동일한 값을 곱하는 경우를 의미합니다. 그리고 그 자체로 곱해진 단일 숫자를 숫자의 제곱근이라고 합니다. 예를 들어, 임의의 숫자 3의 제곱을 구하고 숫자 3에 3 * 3을 곱하여 제곱 9를 반환한다고 가정합니다. 그리고 동일한 숫자 3을 숫자 9의 제곱근이라고 합니다. 마찬가지로, 숫자 81이 있고 그 숫자의 제곱근은 9(9 * 9 = 81)입니다.

C의 제곱근

C 프로그래밍에서 sqrt() 함수는 숫자의 제곱근을 계산하는 데 사용되는 미리 정의된 라이브러리 함수입니다. sqrt() 함수는 math.h 헤더 파일에 정의되어 있습니다. 따라서 C에서는 sqrt() 함수를 사용하면서 헤더 파일을 작성해야 합니다. 또한 sqrt 함수를 사용하지 않고도 주어진 숫자의 제곱근을 구할 수 있습니다.

sqrt() 함수의 구문

 double sqrt( double arg); 

위 구문에서 sqrt() 함수는 단일 인수를 double로 사용하여 double 데이터 유형의 제곱근을 반환합니다.

인수: sqrt() 함수의 이중 데이터 유형 인수입니다.

반환 값: sqrt 함수는 정의된 double 데이터 유형에서 주어진 숫자의 제곱근을 반환합니다.

이 xd는 무슨 뜻인가요?

참고: 주어진 데이터 유형을 다른 데이터 유형으로 명시적으로 변환하여 int, float, double 또는 long double 데이터 유형 숫자의 제곱근을 찾을 수 있습니다.

제곱근을 찾는 알고리즘

  1. 정수 변수를 num으로 선언합니다.
  2. sqrt() 함수를 사용하여 num 변수를 인수로 전달하여 제곱근을 찾습니다.
  3. 결과를 인쇄하세요.
  4. 프로그램을 종료하거나 종료합니다.

예제 1: sqrt() 함수를 사용하여 숫자의 제곱근을 구하는 프로그램

C에서 sqrt() 함수를 사용하여 주어진 숫자의 제곱근을 계산하는 예를 고려해 보겠습니다.

 #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; } 

산출:

 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: 사용자로부터 숫자를 가져와서 제곱근을 구하는 프로그램

사용자로부터 입력을 받아 숫자의 제곱근을 인쇄한 다음 C에서 sqrt() 함수를 사용하는 예를 고려해 보겠습니다.

팩토리얼 자바
 #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; } 

산출:

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

예제 3: 사용자 정의 함수를 사용하여 제곱근을 구하는 프로그램

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; } 

산출:

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

pow() 함수

pow()는 주어진 숫자의 거듭제곱을 계산하기 위해 math.h 헤더 파일에 미리 정의된 함수입니다.

pow() 함수의 구문

 int pow( arg, 0.5); 

pow() 함수는 두 개의 인수를 사용합니다. 첫 번째 인수는 거듭제곱 또는 주어진 숫자의 제곱근을 얻기 위한 변수를 정의하고 0.5는 ½ 또는 1/2 = 0.5와 동일한 기본 인수입니다.

예제 4: pow() 함수를 사용하여 숫자의 제곱근을 구하는 프로그램

C에서 pow() 함수를 사용하여 숫자의 제곱근을 인쇄하는 예를 고려해 보겠습니다.

 #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; } 

산출:

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

위 프로그램에서 변수 x의 정수 값은 사용자로부터 1225이고 x를 pow() 함수에 인수로 전달하여 주어진 숫자의 거듭제곱 또는 제곱근을 반환합니다.

예제 5: sqrt() 함수를 사용하지 않고 숫자의 제곱근을 구하는 프로그램

C에서 미리 정의된 sqrt() 함수를 사용하지 않고 숫자의 제곱근을 인쇄하는 예를 고려해 보겠습니다.

 /* 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; } 

산출:

자바의 지도
 Enter a number to get the square root: 2 The square root of 2 is 1.414214 

위 프로그램에서는 사용자로부터 제곱근을 구하는 숫자를 입력했습니다. 따라서 먼저 주어진 숫자를 2로 나누어 sqrt 변수에 저장합니다. 그런 다음 temp를 0으로 초기화합니다. 그런 다음 계속 반복하여 sqrt가 temp와 같지 않은지 확인하는 while 루프를 사용하고, 각 반복마다 sqrt 값을 temp에 할당하고 sqrt는 다음을 해결하여 새로운 값을 얻습니다. 논리(숫자/온도 + 온도) /2; 그런 다음 2의 제곱근은 1.414214가 됩니다.