简体   繁体   中英

Regarding Floating Point Numbers

I know "homework" isn't perceived well on here but I've tried searching online but I'm not really getting anywhere and I'm not asking anyone to complete it, just to point me in the right direction.

I've created this over the course of the last few hours, not much I know but so far so good. Now I have to add to it so it prints two floats after the previous two results (The first sum and the modulo sum). For example, it won't print 4 for 30/7, but rather 4.28 or whatever. TIA :)

    #include <stdio.h>
    int main()
    {
    int number1, number2, sum; //declares 3 variables
    printf("This is used to divide and find the modulo of two integers\n");
    printf("Enter your first integer: ");
    scanf("%d", &number1);
    printf("Enter your second integer: \n");
    scanf("%d", &number2);
    sum = number1 / number2; 
    printf("%d / %d = %d\n", number1, number2, sum);
    sum = number1 % number2; 
    printf("%d / %d = %\n", number1, number2, sum); 


return 0;

}

To print a float use %f and convert the number to float :

printf("%d / %d = %f\n", number1, number2, (float)number1 / number2);
//                 ^                       ^^^^^^^

Important things to note about this change (highlighted above):

  • Use %f for a float or a double
  • Cast one of the numbers to float or a double to make a floating point division.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM