Monday, February 8, 2016

Reading two float numbers, assign sum to integer variable.

/* C program to read two floating point numbers assign sum to integer variable and then print all three values on screen. */

#include<stdio.h>
int main()
{
float f1,f2;
int total;

printf("Enter your floating point number1:");
scanf("%f",&f1);

printf("Enter your floating point number2:");
scanf("%f",&f2);

total = f1 + f2;

printf("Total is: %d ",total);
   return 0;
}

Output of program

Enter your floating point number1:10.4
Enter your floating point number2:20.3
Total is: 30


2 comments:

  1. Why does the result show 30 instead of 31?

    ReplyDelete
    Replies
    1. Very good observation.

      In C language, when float to int conversion happens, it “rounds toward zero” (i.e. drops the fractional part from the float number).

      Here FLOAT to INT conversion will not happen like traditional ROUNDING function of Maths.

      Delete