Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Sunday, June 14, 2020

Program to find square root



C Program to find square root of a number:
By Ahmad Irshad | Programming Exercises and Solutions > find square root of a number

Write a C program to find square root of a given number using sqrt() funciton (This function is used to find square root of a given number). Now let's see, how  to use this sqrt() funciton in programming.

Required knowledge for this exercise is

Before writing any program you just imagine about output screen first. For example: what input to be given, and output to be displayed. Let's have a look at the below example: 

INPUT:

Enter any number to find square root: 625

OUTPUT:

Square root of 625.00 is = 25.00


Logic to find square root of a given number:

Step by step logic to find square root of a given number.

1) Print a massage to the user like "Enter any number to find square root:"  using printf();
2) Input a number using scanf(); and store it in a variable num.
3) Now calculate square root using built-in function sqrt();
4) At last print the result of square root. that's it!

Program to calculate square root of a given number using sqrt() function: 

  1. /* C program to find square root of a given number */ #include <stdio.h> #include <math.h>
  2. int main() { double num, sroot; /* * Input a number from user */
  3. printf("Enter any number to find square root: "); scanf("%lf", &num); /* * Calculate square of a given number */ sroot = sqrt(num);
  4. /* * Print the result of sqaure root */ printf("Square root of %.2lf is = %.2lf", num, sroot); return 0; }

Output:

  1. Enter any number to find square root: 625
  1. Square root of 625.00 is = 25.00

Note: We use header file #include <math.h> since, it contains the declaration of sqrt() function. Visit to more about function and what is header file.

In above example, statements scanf("%lf", &num); format specifier %lf is used for double data type. 

And statements, printf("Square root of %.2lf is = %.2lf", num, sroot); %.2lf is used to print the number in decimal format upto 2 digits after dot. For Example: 625.00, 25.00 etc. 

Visit to know more about Format specifier.



Next Topic to be updated soon..>>




Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us