d991
d991

Reputation: 23

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;

}

Upvotes: 2

Views: 56

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727057

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.

Upvotes: 3

Related Questions