Sunday, June 14, 2020
Program to find square root
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:
Output:
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.
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
Sunday, June 14, 2020
C
Subscribe to:
Post Comments (Atom)
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.