C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C Prime Number code in C Factorial Program in C using For Loop Factorial Program in C Using While Loop Fibonacci Series in C Using For Loop Fibonacci series in C using while loop Prime Number Program in C using for Loop While Loop in C programming examples Built-in functions in C Assert() Function C vs Java Strings Call Back Function in Embedded C Else If Ladder fgets() function Ftell() Function getc() function getch() function gets() function Heap Sort Nested if-else statement Pi() Function Positioning of file Write() function abs() function in C Attributes in C C program to find factorial of a number using Recursion Ferror() in c fopen() function in C Fibonacci series program in C using Recursion Formatted Input and output function in C Snake Game in C User Defined Functions in C Beep() function in C Cbrt() function in C Hook() function in C Isalnum() function in C C Program to find the Roots of a Quadratic Equation C Switch Statements Difference between rand() and srand() function in C Difference between while and for loop in C Doubly Linked list in C Example of Iteration in C How to use atoi() function in C How to use floor() function in C How to use sine() function in C How to use Typedef Struct in C Integer Promotions in C C Program Swap Numbers in cyclic order Using Call by Reference C Program to Find Largest Number Using Dynamic Memory Allocation C Program to Find the Largest Number using Ternary Operator C/C++ Program to Find the Size of int, float, double and char Find the Largest Three Distinct Elements in an Array using C/C++ Loop Questions in C Modulus on Negative Numbers in C Multiplication table program in C using For loop Nested Loops in C Programming Examples C Program for Mean and Median of an Unsorted Array Results of Comparison Operations in C and C++ Reverse a Stack using Recursion in C Simple hash() function in C strcat() Function in C Sum of N numbers in C using For loop Use of free() function in C Write a program that produces different results in C and C++ C Function Argument and Return Values Keywords in C Bank management system in C Calendar application in C Floor() Function in C Free() Function in C How to delete a file in C How to move a text in C Remove an element from an array in C Unformatted input() and output() function in C What are linker and loader in C

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.



ADVERTISEMENT
ADVERTISEMENT