C/Data Type/Float

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Average the items in a 5 element array

#include <stdio.h>

int main() {
    float data[5];
    float total;
    float average;

    data[0] = 3.0;
    data[1] = 2.0;
    data[2] = 4.0;
    data[3] = 8.0;
    data[4] = 2.0;
    total = data[0] + data[1] + data[2] + data[3] + data[4];
    average =  total / 5.0;
    printf("Total %f Average %f\n", total, average);
    return (0);
}


Calculation between int and float

  
#include <stdio.h>
int main(void)
{
  int i;
  float f;
  i = 10; 
  f = 2.25;
  printf("%f", i * f);
  return 0;
}


Calculation: float

#include <stdio.h>
float area(int width, float height) {
    return (width * height);
}
int main()
{
    float size = area(3.0, 20);
    printf("Area is %f\n", size);
    return (0);
}


Computes the accuracy of the floating point numbers in storage and calculations

#include <stdio.h>
int main()
{
    float floatNumber1, floatNumber2;
    float result;
    int   counter;
    floatNumber1 = 1.0;
    floatNumber2 = 1.0;
    counter = 0;
    while (floatNumber1 + floatNumber2 != floatNumber1) {
        ++counter;
        floatNumber2 = floatNumber2 / 10.0;
    }
    printf("%2d digits accuracy in calculations\n", counter);
    floatNumber2 = 1.0;
    counter = 0;
    while (1) {
        result = floatNumber1 + floatNumber2;
        if (result == floatNumber1)
            break;
        ++counter;
        floatNumber2 = floatNumber2 / 10.0;
    }
    printf("%2d digits accuracy in storage\n", counter);
    return (0);
}


Do calculation in printf: float value

#include <stdio.h>
int main(void)
{
  printf("Number of seconds in a year: ");
  printf("%f", 60.0 * 60.0 * 24.0 * 365.0);
  return 0;
}


Floating Point Input

#include <stdio.h>
void main()
{
  float fp1 = 1.0f;
  float fp2 = 20.0f;
  float fp3 = 440.0f;
  int k = 0;   
  k = scanf("%f %f %f", &fp1, &fp2, &fp3);
  printf("\nReturn value = %d", k);
  printf("\nfp1 = %f  fp2 = %f  fp3 = %f\n", fp1, fp2, fp3);
}


Output float

  
#include <stdio.h>
int main(void)
{
  printf("%f", 2309);
  return 0;
}


Print i and i/2 with fractions

#include <stdio.h>
int main(void) 
{
  int i;
  for(i=1; i<=100; ++i)
    printf("%d / 2 is: %f\n", i, (float) i /2);
  return 0;
}


Read user input: float

#include <stdio.h>
void main()
{
   float diameter = 0.0f;            /* The diameter of the table      */
   printf("Input the diameter of the table:");
   
   scanf("%f", &diameter);           /* Read the diameter from the keyboard */
   
   printf("\nThe input is %f\n", diameter);  
}


Save calculation result into a float variable

  
#include <stdio.h>
int main(void)
{
  float f;
  f = 10 / 3;
  printf("%f", f);
  return 0;
}


Use float as for loop control variable

#include <stdio.h>
int main(void)
{
  double f;
  for(f = 1.0; f < 1.0e+10; f = f*10)
    printf("%g ", f);
  return 0;
}


Use scanf and printf to read and write float value in console

#include <stdio.h>
int main(void)
{
  float a, b;
  printf("Enter two numbers: ");
  scanf("%f", &a);
  scanf("%f", &b);
  printf(" sum = %f.", a + b);
  return 0;
}


Working with float

#include <stdio.h>
int main(void)
{
  float weight;
  printf("Enter your weight: ");
  scanf("%f", &weight);
  printf("weight on the moon: %f", weight * 0.17);
  return 0;
}


Work with int and float

  
#include <stdio.h>
int main(void)
{
  int num;
  float f;
  printf("Enter an integer: ");
  scanf("%d", &num);
  printf("Enter a floating point number: ");
  scanf("%f", &f);
  printf("%d ", num);
  printf("%f", f);
  return 0;
}