×

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

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 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.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Factorial Program in C Using While Loop

Before looking into the mechanism of factorial program, first, you have to understand about the working of a while loop. While Loop The syntax that has been used for the “while” loop...

4 minutes read.

Types of Pointers in C

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

4 minutes read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 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.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.