×

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number.

A square root is a mathematical term. The square root of a number is the inverse of the square of the number. A number's square root is a value that yields the original number when multiplied by itself.

It describes returning the square of a number when the same is multiplied by itself.

Example

Let us take the number 4; 4 multiplied by 4 is 16.

4 * 4 = 16.

In the above case, 4 is the square root for the number 16.

Similarly, let us take another number, 2. 2 multiplied by 2 is 4.

2 * 2 = 4.

In the above case, 2 is the square root for the number 4.

What is sqrt() function?

The sqrt() function is used in the C language to determine the square root of an integer.

The sqrt() function is a built-in library function in the C programming language used to determine a number's square root. The math. h header file defines the sqrt() function. Therefore, to use the sqrt() function in C, we must create the math.h header file. In addition, we don't need to use the sqrt function to determine the supplied number's square root.

Syntax

double sqrt(double x);

Here, the argument is x. It is a number used to calculate the square root of x.

The sqrt() method takes a single double datatype input and returns the square root as a double data type using the above syntax.

By explicitly converting the provided data type to another, it is possible to determine the square root of a number of the int, float, double, or long double data types.

Algorithm for Finding the Square Root

  1. Create an integer variable called "n."
  2. To determine the square root, use the variable “n” as an argument in the sqrt() function.
  3. Print the outcome.
  4. Exit or close the program.

Example 1

In this program, the output is the square root of the number 100.

#include <stdio.h>  
#include <math.h>  
  
int main ()  
{  
    // declaring an integer variable. Here, the variable is named "a."
    int a = 100 ;  
    double result ;  
    // use the sqrt() function to return integer values   
    result = sqrt(a) ;   
    printf (" \n The square root of the number %d is: %.2lf", a, result) ;  
  
    return 0 ;     
}  

Output

How to find square root in C Language

In the above-displayed screenshot, the output is the square root of the number 100. The value 10 is the square root of the number 100.

Example 2

A program calculates a number's square root using the sqrt() function.

In this program, the user gives the input for int, float, and double datatypes.

#include <stdio.h>  
#include <math.h>  
  
int main ()  
{  
    // the int, double, and float variables are declared
    int a, result ;  
    float b, result1 ;  
    double c, result2 ;  
    
    printf (" Enter the numbers to find their square root: ") ;  
    scanf("%d",&a) ;
    // sqrt() function is used. Here, it returns an integer value.
    result = sqrt(a) ;   
    printf ("The square root of the number %d is: %d", a, result) ;  
    scanf("%f",&b) ;
    // finding square root for the floating-point values.
    // sqrt() function is used. Here, it returns the float value.
    result1 = sqrt(b) ;   
    printf (" \nThe square root of %.2f is: %.2f", b, result1) ;  
          
           // finding square root for the floating-point values for the double datatype.
    scanf("%lf",&c) ;
    // sqrt() function is used. Here, it returns the double value.
    result2 = sqrt(c) ;   
    printf (" \nThe square root of %.2lf is: %.2lf", c, result2) ;  
      
    return 0 ;     
}  


The above program represents the sqrt() function.

Output 1

How to find square root in C Language

In the above-displayed screenshot, the user input is 9 (int value), 36.00 (float value), and 64.0000 (double value).

The value 3 is the square root for the value 9.

The value 6 is the square root for the value 36.

The value 8 is the square root for the value 64.

Output 2

How to find square root in C Language

In the above-displayed screenshot, the user input is 4 (int value), 2.25 (float value), and 6.2500 (double value).

The value 2 is the square root for the value 4.

The value 1.50 is the square root for the value 2.25.

The value 2.50 is the square root for the value 6.25.

Output 3

How to find square root in C Language

In the above-displayed screenshot, the user input is 25 (int value), 3.00 (float value), and 64.6400 (double value).

The value 5 is the square root for the value 25.

The value 1.73 is the square root for the value 3.00.

The value 8.04 is the square root for the value 64.64.

Example 3

Let's write a program in the C programming language that uses the user-defined function to calculate the value's square root.

#include <stdio.h>  
#include <math.h>  
  
// declaring the function Sqrtfunc().
double Sqrtfunc (int n) ;  
  
int main ()  
{  
    // declaring an integer variable x. 
    int x ;  
    double result ;  
    printf (" Enter any number to get the square root: ") ;  
    scanf (" %d", &x) ;  
  
    result = Sqrtfunc(x) ; // calling the function  Sqrtfunc().
    printf (" \n The square root of  %d is: %.2lf", x, result) ;  
  
    return 0 ;     
}  
  
// defining a function Sqrtfunc()
double  Sqrtfunc ( int n)  
{  
    double Ans ;  
    // using the sqrt() function to print the square root of the number.
    Ans =  sqrt(n) ;  
    return Ans ;  
}  

Output 1

How to find square root in C Language

In the above-displayed screenshot, the user input is the number 225. The square root for the number 225 is 15.

Output 2

How to find square root in C Language

In the above-displayed screenshot, the user input is the number 729. The square root for the number 729 is 27.

The pow() function in C programming

Calculating the square root of a number is possible with the pow() function too.

Syntax

int pow(x, 0.5) ;

Two arguments are required by the pow() function: the first argument defines a variable to calculate the power or square root of the provided value, and the default argument, 0.5, is equal to 1/2 = 0.5. (½)

1) Example of pow() function

Let's look at an example utilizing the pow() function to display the square root of a value.

#include <stdio.h>  
#include <math.h>  
  
int main ()  
{  
    // declare an integer variable  
    int a ;  
    double result ;  
    printf (" Enter the number to get the square root of that number: ") ;  
    scanf (" %d", &a) ;  
    // using the pow() function to return the square root of that number.   
    result = pow(a, 0.5) ; 
    //Two arguments are required: an input variable and the default value of 0.5.
    printf (" \n The square root of %d is: %.2lf", a, result) ;  
  
    return 0 ;     
}

Output 1

How to find square root in C Language

In the above-displayed screenshot, the user input is the number 49. The square root for the number 49 is 7.

Output 2

How to find square root in C Language

In the above-displayed screenshot, the user input is the number 36. The square root for the number is 6.36.

2) An example of printing a number's square root without utilizing the sqrt() function with C programming.

/* Program for the square root of a number without using the sqrt() function in C programming*/  
#include <stdio.h>  
int main()  
{  
    // declaration of the variables n, temp, sqrt. 
    int n ;  
    float sqrt, temp ;  
      
    printf (" Enter the number to get the square root of that number: ") ;  
    scanf (" %d", &n) ;  
      
    // divide the given number n by 2 and store it in the sqrt variable.
    sqrt = n / 2 ;  
    temp = 0 ;  
      
    //  Use a while loop to repeatedly check that the variable sqrt and 
   //variable temp is not the same.
    while (sqrt != temp) // sqrt = n and temp is first set to 0.
    {  
        temp = sqrt ; // assigning sqrt value into temp variable
          
        sqrt = ( n / temp + temp) / 2 ;  
     }   
       
     printf (" \n The square root of the number %d is %f", n, sqrt) ;  
     return 0 ;  
}  

Output 1

How to find square root in C Language

In the above-displayed screenshot, the user input is the number 64. The square root for the number is 8.

Output 2

How to find square root in C Language

In the above-displayed screenshot, the user input is the number 3. The square root for the number is 1.732051.

Output 3

How to find square root in C Language

In the above-displayed screenshot, the user input is the number 6. The square root for the number is 2.449490.


Related Topics

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.