C Tutorial/Data Type/float Calculation
Division with float values
<source lang="cpp">#include <stdio.h> int main(void) {
float plank_length = 10.0f; float piece_count = 4.0f; float piece_length = 0.0f; piece_length = plank_length/piece_count; printf("A plank %f feet long can be cut into %f pieces %f feet long.", plank_length, piece_count, piece_length); return 0;
}</source>
A plank 10.000000 feet long can be cut into 4.000000 pieces 2.500000 feet long.
Do a calculation on float number
<source lang="cpp">#include <stdio.h>
- include <stdlib.h>
int main() {
float height_in_cm; char height_in_inches[4]; printf("Enter your height in inches:"); gets(height_in_inches); height_in_cm = atoi(height_in_inches) * 2.54; printf("You are %.2f centimeters tall.\n",height_in_cm); return(0);
}</source>
Enter your height in inches:1 You are 2.54 centimeters tall.
Read float number and do a calculation
<source lang="cpp">#include <stdio.h> int main(void) {
float radius = 0.0f; float diameter = 0.0f; float circumference = 0.0f; float area = 0.0f; float Pi = 3.14159265f; printf("Input the diameter of the table:"); scanf("%f", &diameter); radius = diameter/2.0f; circumference = 2.0f*Pi*radius; area = Pi*radius*radius; printf("\nThe circumference is %.2f", circumference); printf("\nThe area is %.2f\n", area); return 0;
}</source>
Input the diameter of the table:123 The circumference is 386.42 The area is 11882.29