Working with floats from user input in C - Printable Version +- QP School (https://qomplainerzschool.lima-city.de) +-- Forum: Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=3) +--- Forum: C 18 Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=32) +--- Thread: Working with floats from user input in C (/showthread.php?tid=5144) |
Working with floats from user input in C - Qomplainerz - 07-26-2023 Example: float radius; printf("Enter the radius of a circle: "); scanf("%f", &radius); float area = 3.14159 * radius * radius; printf("The area of the circle is: %.2f\n", area); Explanation: In this example, we declare a float variable radius to store the user's input. We prompt the user to enter the radius of a circle using printf(). We use the %f format specifier in scanf() to read a floating-point value from the user and store it in the radius variable. We calculate the area of the circle using the formula area = π * r^2, where π is approximated as 3.14159. Finally, we print the calculated area using printf(). The .2 in the format specifier %.2f ensures that the area is displayed with two decimal places. |