# float

***

**1. Calculating Average**

```c
#include <stdio.h>

int main() {
    float num1, num2, avg;

    printf("Enter two numbers: ");
    scanf("%f %f", &num1, &num2);

    avg = (num1 + num2) / 2.0f;

    printf("Average: %.2f\n", avg);

    return 0;
}
```

**2. Converting Integer to Float**

```c
#include <stdlib.h>

int main() {
    int num = 10;
    float fnum;

    fnum = (float)num;

    printf("Float number: %.2f\n", fnum);

    return 0;
}
```

**3. Converting String to Float**

```c
#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[] = "12.34";
    float fnum;

    fnum = atof(str);

    printf("Float number: %.2f\n", fnum);

    return 0;
}
```

**4. Rounding a Float**

```c
#include <math.h>

int main() {
    float fnum = 12.345;

    printf("Rounded float: %.2f\n", round(fnum));

    return 0;
}
```

**5. Truncating a Float**

```c
#include <math.h>

int main() {
    float fnum = 12.345;

    printf("Truncated float: %f\n", trunc(fnum));

    return 0;
}
```

**6. Checking if a Number is NaN (Not-a-Number)**

```c
#include <math.h>

int main() {
    float fnum = NAN;

    if (isnan(fnum)) {
        printf("The number is NaN.\n");
    } else {
        printf("The number is not NaN.\n");
    }

    return 0;
}
```

**7. Checking if a Number is Infinite**

```c
#include <math.h>

int main() {
    float fnum = INFINITY;

    if (isinf(fnum)) {
        printf("The number is infinite.\n");
    } else {
        printf("The number is not infinite.\n");
    }

    return 0;
}
```

**8. Calculating Trigonometric Functions**

```c
#include <math.h>

int main() {
    float angle = 45.0f;

    printf("Sine of 45 degrees: %.2f\n", sin(angle * M_PI / 180));
    printf("Cosine of 45 degrees: %.2f\n", cos(angle * M_PI / 180));
    printf("Tangent of 45 degrees: %.2f\n", tan(angle * M_PI / 180));

    return 0;
}
```

**9. Calculating Exponents and Logarithms**

```c
#include <math.h>

int main() {
    float base = 2.0f;
    float exponent = 3.0f;

    printf("2 to the power of 3: %.2f\n", pow(base, exponent));
    printf("Logarithm of 8 to the base 2: %.2f\n", log2(8.0f));

    return 0;
}
```

**10. Generating Random Float Numbers**

```c
#include <stdlib.h>

int main() {
    float fnum;

    fnum = (float)rand() / (float)RAND_MAX;

    printf("Random float: %.2f\n", fnum);

    return 0;
}
```

**11. Converting Float to Integer**

```c
#include <stdlib.h>

int main() {
    float fnum = 12.34f;
    int inum;

    inum = (int)fnum;

    printf("Integer number: %d\n", inum);

    return 0;
}
```

**12. Parsing a Float from a String**

```c
#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[] = "12.34";
    float fnum;

    fnum = strtof(str, NULL);

    printf("Float number: %.2f\n", fnum);

    return 0;
}
```

**13. Printing a Float as a String**

```c
#include <stdio.h>
#include <stdlib.h>

int main() {
    float fnum = 12.34f;
    char str[100];

    sprintf(str, "%.2f", fnum);

    printf("Float number as a string: %s\n", str);

    return 0;
}
```

**14. Comparing Float Numbers**

```c
#include <stdio.h>

int main() {
    float num1 = 12.34f;
    float num2 = 12.345f;

    if (num1 == num2) {
        printf("The numbers are equal.\n");
    } else {
        printf("The numbers are not equal.\n");
    }

    return 0;
}
```

**15. Formatting Float Numbers**

```c
#include <stdio.h>

int main() {
    float fnum = 1234.56789f;

    printf("Formatted float: %.2f\n", fnum);

    return 0;
}
```

**16. Calculating the Area of a Circle**

```c
#include <stdio.h>
#include <math.h>

int main() {
    float radius = 5.0f;
    float area;

    area = M_PI * radius * radius;

    printf("Area of a circle: %.2f\n", area);

    return 0;
}
```

**17. Calculating the Volume of a Sphere**

```c
#include <stdio.h>
#include <math.h>

int main() {
    float radius = 5.0f;
    float volume;

    volume = (4.0f / 3.0f) * M_PI * pow(radius, 3);

    printf("Volume of a sphere: %.2f\n", volume);

    return 0;
}
```

**18. Calculating the Surface Area of a Sphere**

```c
#include <stdio.h>
#include <math.h>

int main() {
    float radius = 5.0f;
    float surface_area;

    surface_area = 4.0f * M_PI * pow(radius, 2);

    printf("Surface area of a sphere: %.2f\n", surface_area);

    return 0;
}
```

**19. Calculating the Distance Between Two Points**

```c
#include <stdio.h>
#include <math.h>

int main() {
    float x1 = 1.0f;
    float y1 = 2.0f;
    float x2 = 4.0f;
    float y2 = 6.0f;
    float distance;

    distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));

    printf("Distance between two points: %.2f\n", distance);

    return 0;
}
```

**20. Calculating the Slope of a Line**

```c
#include <stdio.h>

int main() {
    float x1 = 1.0f;
    float y1 = 2.0f;
    float x2 = 4.0f;
    float y2 = 6.0f;
    float slope;

    slope = (y2 - y1) / (x2 - x1);

    printf("Slope of a line: %.2f\n", slope);

    return 0;
}
```

**21. Solving a Quadratic Equation**

```c
#include <stdio.h>
#include <math.h>

int main() {
    float a = 1.0f;
    float b = -5.0f;
    float c = 6.0f;
    float root1, root2;

    root1 = (-b + sqrt(pow(b, 2) - 4 * a * c)) / (2 * a);
    root2 = (-b - sqrt(pow(b, 2) - 4 * a * c)) / (2 * a);

    printf("Roots of a quadratic equation: %.2f, %.2f\n", root1,

```
